NAME

Perinci::Sub::Util - Helper when writing functions

VERSION

version 0.35

SYNOPSIS

use Perinci::Sub::Util qw(wrapres caller);

sub foo {
    my %args = @_;
    my $res;

    my $caller = caller();

    $res = bar(); # call another function
    return wrapres([500, "Can't bar: "], $res) unless $res->[0] == 200;
    $res = baz();
    return wrapres([500, "Can't baz: "], $res) unless $res->[0] == 200;

    [200, "OK"];
}

FUNCTIONS

caller([ $n ])

Just like Perl's builtin caller(), 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.

wrapres([$status, $msg, $result, $meta], $res) => ARRAY

Generate an envelope response based on an existing $res, with an option to build an error stack. Usually used to create an error response which preserves inner error.

If $status is undefined, will use $res's status.

If $message is undefined, will use $res's message. If $message ends with /:\s?\z/, will append $res's message.

If $result is undefined, will use $res's result.

If $meta is undefined, empty default is used. If instructed to build an error stack, will append $res to result metadata's error_stack.

Error stack by default is off. Can be turned on via setting global variable $Perinci::ERROR_STACK to true value, or environment variable PERINCI_ERROR_STACK, or package variables $PACKAGE::PERINCI_ERROR_STACK (to turn on on a package basis).

Some examples ($res is assumed to be [404, "not found"]:

wrapres(undef, $res);
# when error stack is off: [404, "not found"]
# when error stack is on : [404, "not found", undef,
#                          {error_stack=>[404, "not found"]}]

wrapres([undef, "can't select user: "], $res);
# when error stack is off: [404, "can't select user: 404 - not found"]
# when error stack is on : [405, "can't select user: 404 - not found", undef,
                            {error_stack=>[404, "not found"]}]

wrapres([500, "can't select user", -1, {foo=>1}], $res);
# when error stack is off: [500, "can't select user"]
# when error stack is on : [500, "can't select user", -1,
                            {foo=>1, error_stack=>[404, "not found"]}]

SEE ALSO

Perinci::Util

Perinci

AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Steven Haryanto.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.