.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Perinci::Sub::Util 3pm" .TH Perinci::Sub::Util 3pm "2020-10-24" "perl v5.30.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Perinci::Sub::Util \- Helper when writing functions .SH "VERSION" .IX Header "VERSION" This document describes version 0.470 of Perinci::Sub::Util (from Perl distribution Perinci-Sub-Util), released on 2020\-10\-20. .SH "SYNOPSIS" .IX Header "SYNOPSIS" Example for \fBerr()\fR and \fBcaller()\fR: .PP .Vb 1 \& use Perinci::Sub::Util qw(err caller); \& \& sub foo { \& my %args = @_; \& my $res; \& \& my $caller = caller(); \& \& $res = bar(...); \& return err($err, 500, "Can\*(Aqt foo") if $res\->[0] != 200; \& \& [200, "OK"]; \& } .Ve .PP Example for \fBdie_err()\fR and \fBwarn_err()\fR: .PP .Vb 3 \& use Perinci::Sub::Util qw(warn_err die_err); \& warn_err(403, "Forbidden"); \& die_err(403, "Forbidden"); .Ve .PP Example for \fBgen_modified_sub()\fR: .PP .Vb 1 \& use Perinci::Sub::Util qw(gen_modified_sub); \& \& $SPEC{list_users} = { \& v => 1.1, \& args => { \& search => {}, \& is_suspended => {}, \& }, \& }; \& sub list_users { ... } \& \& gen_modified_sub( \& output_name => \*(Aqlist_suspended_users\*(Aq, \& base_name => \*(Aqlist_users\*(Aq, \& remove_args => [\*(Aqis_suspended\*(Aq], \& output_code => sub { \& list_users(@_, is_suspended=>1); \& }, \& ); .Ve .PP Example for \fBgen_curried_sub()\fR: .PP .Vb 1 \& use Perinci::Sub::Util qw(gen_curried_sub); \& \& $SPEC{list_users} = { \& v => 1.1, \& args => { \& search => {}, \& is_suspended => {}, \& }, \& }; \& sub list_users { ... } \& \& # simpler/shorter than gen_modified_sub, but can be used for currying only \& gen_curried_sub(\*(Aqlist_users\*(Aq, {is_suspended=>1}, \*(Aqlist_suspended_users\*(Aq); .Ve .SH "FUNCTIONS" .IX Header "FUNCTIONS" .SS "gen_curried_sub" .IX Subsection "gen_curried_sub" Usage: .PP .Vb 1 \& gen_curried_sub( [ \e%optional_named_args ] , $base_name, $output_name) \-> any .Ve .PP Generate curried subroutine (and its metadata). .PP This is a more convenient helper than \f(CW\*(C`gen_modified_sub\*(C'\fR if you want to create a new subroutine that has some of its arguments preset (so they no longer need to be present in the new metadata). .PP For more general needs of modifying a subroutine (e.g. add some arguments, modify some arguments, etc) use \f(CW\*(C`gen_modified_sub\*(C'\fR. .PP This function is not exported by default, but exportable. .PP Arguments ('*' denotes required arguments): .IP "\(bu" 4 \&\fB\f(CB$base_name\fB\fR* => \fIstr\fR .Sp Subroutine name (either qualified or not). .Sp If not qualified with package name, will be searched in the caller's package. Rinci metadata will be searched in \f(CW%SPEC\fR package variable. .IP "\(bu" 4 \&\fB\f(CB$output_name\fB\fR* => \fIstr\fR .Sp Where to install the modified sub. .Sp Subroutine will be put in the specified name. If the name is not qualified with package name, will use caller's package. .IP "\(bu" 4 \&\fBset_args\fR => \fIhash\fR .Sp Arguments to set. .PP Return value: (any) .SS "gen_modified_sub" .IX Subsection "gen_modified_sub" Usage: .PP .Vb 1 \& gen_modified_sub(%args) \-> [status, msg, payload, meta] .Ve .PP Generate modified metadata (and subroutine) based on another. .PP Often you'll want to create another sub (and its metadata) based on another, but with some modifications, e.g. add/remove/rename some arguments, change summary, add/remove some properties, and so on. .PP Instead of cloning the Rinci metadata and modify it manually yourself, this routine provides some shortcuts. .PP You can specify base sub/metadata using \f(CW\*(C`base_name\*(C'\fR (string, subroutine name, either qualified or not) or \f(CW\*(C`base_code\*(C'\fR (coderef) + \f(CW\*(C`base_meta\*(C'\fR (hash). .PP This function is not exported by default, but exportable. .PP Arguments ('*' denotes required arguments): .IP "\(bu" 4 \&\fBadd_args\fR => \fIhash\fR .Sp Arguments to add. .IP "\(bu" 4 \&\fBbase_code\fR => \fIcode\fR .Sp Base subroutine code. .Sp If you specify this, you'll also need to specify \f(CW\*(C`base_meta\*(C'\fR. .Sp Alternatively, you can specify \f(CW\*(C`base_name\*(C'\fR instead, to let this routine search the base subroutine from existing Perl package. .IP "\(bu" 4 \&\fBbase_meta\fR => \fIhash\fR .Sp Base Rinci metadata. .IP "\(bu" 4 \&\fBbase_name\fR => \fIstr\fR .Sp Subroutine name (either qualified or not). .Sp If not qualified with package name, will be searched in the caller's package. Rinci metadata will be searched in \f(CW%SPEC\fR package variable. .Sp Alternatively, you can also specify \f(CW\*(C`base_code\*(C'\fR and \f(CW\*(C`base_meta\*(C'\fR. .IP "\(bu" 4 \&\fBdescription\fR => \fIstr\fR .Sp Description for the mod subroutine. .IP "\(bu" 4 \&\fBinstall_sub\fR => \fIbool\fR (default: 1) .IP "\(bu" 4 \&\fBmodify_args\fR => \fIhash\fR .Sp Arguments to modify. .Sp For each argument you can specify a coderef. The coderef will receive the argument ($arg_spec) and is expected to modify the argument specification. .IP "\(bu" 4 \&\fBmodify_meta\fR => \fIcode\fR .Sp Specify code to modify metadata. .Sp Code will be called with arguments ($meta) where \f(CW$meta\fR is the cloned Rinci metadata. .IP "\(bu" 4 \&\fBoutput_code\fR => \fIcode\fR .Sp Code for the modified sub. .Sp Alternatively you can use \f(CW\*(C`wrap_code\*(C'\fR. If both are not specified, will use \&\f(CW\*(C`base_code\*(C'\fR (which will then be required) as the modified subroutine's code. .IP "\(bu" 4 \&\fBoutput_name\fR => \fIstr\fR .Sp Where to install the modified sub. .Sp Subroutine will be put in the specified name. If the name is not qualified with package name, will use caller's package. If no \f(CW\*(C`output_code\*(C'\fR is specified, the base subroutine reference will be assigned here. .Sp Note that this argument is optional. .IP "\(bu" 4 \&\fBremove_args\fR => \fIarray\fR .Sp List of arguments to remove. .IP "\(bu" 4 \&\fBrename_args\fR => \fIhash\fR .Sp Arguments to rename. .IP "\(bu" 4 \&\fBreplace_args\fR => \fIhash\fR .Sp Arguments to add. .IP "\(bu" 4 \&\fBsummary\fR => \fIstr\fR .Sp Summary for the mod subroutine. .IP "\(bu" 4 \&\fBwrap_code\fR => \fIcode\fR .Sp Wrapper to generate the modified sub. .Sp The modified sub will become: .Sp .Vb 1 \& sub { wrap_code\->(base_code, @_) } .Ve .Sp Alternatively you can use \f(CW\*(C`output_code\*(C'\fR. If both are not specified, will use \&\f(CW\*(C`base_code\*(C'\fR (which will then be required) as the modified subroutine's code. .PP Returns an enveloped result (an array). .PP First element (status) is an integer containing \s-1HTTP\s0 status code (200 means \s-1OK,\s0 4xx caller error, 5xx function error). Second element (msg) is a string containing error message, or '\s-1OK\s0' if status is 200. Third element (payload) is optional, the actual result. Fourth element (meta) is called result metadata and is optional, a hash that contains extra information. .PP Return value: (hash) .ie n .SS "caller([ $n ])" .el .SS "caller([ \f(CW$n\fP ])" .IX Subsection "caller([ $n ])" Just like Perl's builtin \fBcaller()\fR, except that this one will ignore wrapper code in the call stack. You should use this if your code is potentially wrapped. See Perinci::Sub::Wrapper for more details. .SS "err(...) => \s-1ARRAY\s0" .IX Subsection "err(...) => ARRAY" Experimental. .PP Generate an enveloped error response (see Rinci::function). Can accept arguments in an unordered fashion, by utilizing the fact that status codes are always integers, messages are strings, result metadata are hashes, and previous error responses are arrays. Error responses also seldom contain actual result. Status code defaults to 500, status message will default to \*(L"\s-1FUNC\s0 failed\*(R". This function will also fill the information in the \f(CW\*(C`logs\*(C'\fR result metadata. .PP Examples: .PP .Vb 6 \& err(); # => [500, "FUNC failed", undef, {...}]; \& err(404); # => [404, "FUNC failed", undef, {...}]; \& err(404, "Not found"); # => [404, "Not found", ...] \& err("Not found", 404); # => [404, "Not found", ...]; # order doesn\*(Aqt matter \& err([404, "Prev error"]); # => [500, "FUNC failed", undef, \& # {logs=>[...], prev=>[404, "Prev error"]}] .Ve .PP Will put \f(CW\*(C`stack_trace\*(C'\fR in logs only if \f(CW\*(C`Carp::Always\*(C'\fR module is loaded. .SS "warn_err(...)" .IX Subsection "warn_err(...)" This is a shortcut for: .PP .Vb 2 \& $res = err(...); \& warn "ERROR $res\->[0]: $res\->[1]"; .Ve .SS "die_err(...)" .IX Subsection "die_err(...)" This is a shortcut for: .PP .Vb 2 \& $res = err(...); \& die "ERROR $res\->[0]: $res\->[1]"; .Ve .SH "FAQ" .IX Header "FAQ" .SS "What if I want to put result ($res\->[2]) into my result with \fBerr()\fP?" .IX Subsection "What if I want to put result ($res->[2]) into my result with err()?" You can do something like this: .PP .Vb 3 \& my $err = err(...) if ERROR_CONDITION; \& $err\->[2] = SOME_RESULT; \& return $err; .Ve .SH "HOMEPAGE" .IX Header "HOMEPAGE" Please visit the project's homepage at . .SH "SOURCE" .IX Header "SOURCE" Source repository is at . .SH "BUGS" .IX Header "BUGS" Please report any bugs or feature requests on the bugtracker website .PP When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature. .SH "SEE ALSO" .IX Header "SEE ALSO" Perinci .SH "AUTHOR" .IX Header "AUTHOR" perlancar .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 2020, 2017, 2016, 2015, 2014 by perlancar@cpan.org. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.