NAME

Sub::Spec::Clause::result - Specify sub result

VERSION

version 1.0.0

SYNOPSIS

# result is an integer
result => 'int*'

# result is an integer starting from zero
result => ['int*' => {ge=>0}]

# result is an array of records
result => ['array*' => {
            summary => 'blah blah blah ...',
            of      => ['hash*' => {allowed_keys=>[qw/name age address/]} ]
          }]

DESCRIPTION

The 'result' clause specifies sub's result value. The value of the clause is a Sah schema, so refer to Data::Sah for more details on the syntax.

Note that since 'result_naked' by default is false, instead of just returning

RESULT_VALUE

you'll normally have to return this from your sub:

[STATUS_CODE, ERR_MSG, RESULT_VALUE]

where STATUS_CODE is a 3-digit number (just like HTTP response status), ERR_MSG contains error message, and RESULT_VALUE is the actual result.

Example:

$SPEC{is_palindrome} = {
    summary => 'Check whether a string is a palindrome',
    args    => {str => 'str*'},
    result  => 'bool*',
};
sub is_palindrome {
    my %args = @_;
    my $str  = $args{str};
    [200, "OK", $str eq reverse($str) ? 1:0];
}

Note that the checking of result value is not implemented yet, it will be implemented by the wrapper code generated by Sub::Spec::Exporter. The wrapper will only check sub's result if status code is successul (200).

SEE ALSO

Sub::Spec

Data::Sah

Sub::Spec::Clause::args

AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2011 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.