Perinc...::Util(3pm)
π NAME π
okay so bestie, this is the manual page for Perinc...::Util(3pm) β aka Perinci::Sub::Util, your little helper buddy when you're out here writing functions. π«Ά
β¨ VERSION β¨
This doc describes version 0.472 of Perinci::Sub::Util, from the Perl distribution Perinci-Sub-Util, released on 2023-10-28. yeah it's been around. it's got lore. π
π SYNOPSIS π
Example for err() and caller(), no cap:
use Perinci::Sub::Util qw(err caller);
sub foo {
my %args = @_;
my $res;
my $caller = caller();
$res = bar(...);
return err($err, 500, "Can't foo") if $res->[0] != 200;
[200, "OK"];
}
Example for die_err() and warn_err() β short and slay:
use Perinci::Sub::Util qw(warn_err die_err);
warn_err(403, "Forbidden");
die_err(403, "Forbidden");
Example for gen_modified_sub() β the glow-up generator:
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 => 'list_suspended_users',
base_name => 'list_users',
remove_args => ['is_suspended'],
output_code => sub {
list_users(@_, is_suspended=>1);
},
);
Example for gen_curried_sub() β same energy, smaller footprint:
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('list_users', {is_suspended=>1}, 'list_suspended_users');
π FUNCTIONS π
π gen_curried_sub
Usage:
gen_curried_sub($base_name, $set_args, $output_name) -> any
Generate a curried subroutine (and its metadata, obviously, she comes as a package deal π).
Lowkey this is just a more convenient helper than gen_modified_sub when all you wanna do is make a new sub with some of its arguments already preset β so those args don't even need to show up in the new metadata anymore. bye bestie π
For more general needs of modifying a subroutine (like, adding arguments, modifying arguments, etc etc) go use gen_modified_sub instead, she's the main character for that.
This function is not exported by default, but it is exportable. π«
Arguments ('*' denotes required arguments):
$base_name*=> str β Subroutine name (either qualified or not). If it's not qualified with a package name, it'll be searched in the caller's package. Rinci metadata gets searched in the%SPECpackage variable. π$output_name=> str β Where to install the modified sub. The subroutine gets put in the specified name. If the name isn't qualified with a package name, it uses the caller's package. And if you don't specify the name at all, it uses the base name β which must not be in the caller's package. β οΈ$set_args*=> hash β Arguments to set. simple as that. β¨
Return value: (any)
π gen_modified_sub
Usage:
gen_modified_sub(%args) -> [$status_code, $reason, $payload, \%result_meta]
Generate modified metadata (and subroutine, the duo π ) based on another one.
fr fr, often you'll want to create another sub (and its metadata) based on another sub, but with some modifications β add/remove/rename some arguments, change the summary, add/remove some properties, and so on. Instead of cloning the Rinci metadata and modifying it manually yourself like some kind of feral gremlin, this routine gives you shortcuts. ate. π
You can specify the base sub/metadata using base_name (string, subroutine name, either qualified or not) or base_code (coderef) + base_meta (hash).
This function is not exported by default, but it is exportable. π«
Arguments ('*' denotes required arguments):
add_args=> hash β Arguments to add. βbase_code=> code β Base subroutine code. If you specify this, you'll also need to specifybase_meta. Alternatively, you can specifybase_nameinstead, and let this routine search the base subroutine from an existing Perl package. π΅οΈbase_meta=> hash β Base Rinci metadata. π¦base_name=> str β Subroutine name (either qualified or not). If it's not qualified with a package name, it'll be searched in the caller's package. Rinci metadata gets searched in the%SPECpackage variable. Alternatively, you can also specifybase_codeandbase_meta. Eitherbase_nameorbase_code+base_metaare required β pick a lane bestie. π¦description=> str β Description for the mod subroutine. πdie=> bool β Die upon failure. dramatic but valid πinstall_sub=> bool (default:1) β (No description) yeah the manpage literally left this one blank, iconic mysterious behavior πmodify_args=> hash β Arguments to modify. For each argument you can specify a coderef. The coderef will receive the argument ($arg_spec) and is expected to modify the argument specification. π§modify_meta=> code β Specify code to modify metadata. Code will be called with arguments ($meta) where$metais the cloned Rinci metadata. πͺoutput_code=> code β Code for the modified sub. Alternatively you can usewrap_code. If both are not specified, it'll usebase_code(which will then be required) as the modified subroutine's code. π§΅output_name=> str β Where to install the modified sub. The output subroutine gets put in the specified name. If the name isn't qualified with a package name, it uses the caller's package. If the name isn't specified, the base name will be used and must not be from the caller's package. Note that this argument is optional. To prevent installing the subroutine, setinstall_subto false. πremove_args=> array β List of arguments to remove. ποΈrename_args=> hash β Arguments to rename. π·οΈreplace_args=> hash β Arguments to add. (yes it says "add", we're quoting the manpage exactly, don't come for me π)summary=> str β Summary for the mod subroutine. βwrap_code=> code β Wrapper to generate the modified sub. The modified sub will become:
sub { wrap_code->(base_code, @_) }
Alternatively you can use output_code. If both are not specified, it'll use base_code (which will then be required) as the modified subroutine's code. π
Returns an enveloped result (an array). π¬
First element ($status_code) is an integer containing an HTTP-like status code (200 means OK, 4xx caller error, 5xx function error). Second element ($reason) is a string containing the error message, or something like "OK" if the status is 200. Third element ($payload) is the actual result, but it's usually not present when the enveloped result is an error response ($status_code is not 2xx). Fourth element (%result_meta) is called result metadata and is optional β a hash that contains extra information, much like how HTTP response headers provide additional metadata. π
Return value: (hash)
π¦ caller([ $n ])
Just like Perl's builtin caller(), except 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. π§
π err(...) => ARRAY
Experimental. β οΈ (yeah, she's in her experimental era)
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 an actual result. Status code defaults to 500, status message will default to "FUNC failed". This function will also fill the information in the "logs" result metadata. π
Examples:
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't matter
err([404, "Prev error"]); # => [500, "FUNC failed", undef,
# {logs=>[...], prev=>[404, "Prev error"]}]
Will put "stack_trace" in logs only if the "Carp::Always" module is loaded. πͺ
πΈ warn_err(...)
This is a shortcut for:
$res = err(...);
warn "ERROR $res->[0]: $res->[1]";
π die_err(...)
This is a shortcut for:
$res = err(...);
die "ERROR $res->[0]: $res->[1]";
π«Ά FAQ π«Ά
What if I want to put result ($res->[2]) into my result with err()?
You can do something like this, bestie:
my $err = err(...) if ERROR_CONDITION;
$err->[2] = SOME_RESULT;
return $err;
π HOMEPAGE π
Please visit the project's homepage at <https://metacpan.org/release/Perinci-Sub-Util>. π‘
π₯ SOURCE π₯
Source repository is at <https://github.com/perlancar/perl-Perinci-Sub-Util>. π»
β SEE ALSO β
Perinci
π AUTHOR π
perlancar <perlancar@cpan.org>
π CONTRIBUTOR π
Steven Haryanto <stevenharyanto@gmail.com>
π¦ CONTRIBUTING π¦
To contribute, you can send patches by email/via RT, or send pull requests on GitHub. π«‘
Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:
% prove -l
If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me. π
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2023, 2020, 2017, 2016, 2015, 2014 by perlancar <perlancar@cpan.org>. π
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. ποΈ
π BUGS π
Please report any bugs or feature requests on the bugtracker website <https://rt.cpan.org/Public/Dist/Display.html?Name=Perinci-Sub-Util>. π
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. (receipts, bestie, we need receipts π§Ύ)
and that's the whole page, periodt π
this was Perinc...::Util(3pm) β generated by perl v5.38.2 on 2024-05-26. go forth and curry your subs, ily π«Άβ¨
π¦ where this page comes from
- debian trixie: libperinci-sub-util-perl 0.472-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libperinci-sub-util-perl 0.472-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libperinci-sub-util-perl 0.472-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms