Return::Value - Polymorphic Return Values
use Return::Value; sub send_over_network { my ($net, $send) = @_: if ( $net->transport( $send ) ) { return success; } else { return failure "Was not able to transport info."; } } my $result = $net->send_over_network( "Data" ); # boolean unless ( $result ) { # string print $result; } sub build_up_return { my $return = failure; if ( ! foo() ) { $return->string("Can't foo!"); return $return; } if ( ! bar() ) { $return->string("Can't bar"); $return->prop(failures => \@bars); return $return; } # we're okay if we made it this far. $return++; return $return; # success! }
Polymorphic return values are really useful. Often, we just want to know if something worked or not. Other times, we'd like to know what the error text was. Still others, we may want to know what the error code was, and what the error properties were. We don't want to handle objects or data structures for every single return value, but we do want to check error conditions in our code because that's what good programmers do.
When functions are successful they may return true, or perhaps some useful data. In the quest to provide consistent return values, this gets confusing between complex, informational errors and successful return values.
This module provides these features with a simple API that should get you what you're looking for in each contex a return value is used in.
The functional interface is highly recommended for use within functions that are using Return::Values.
Return::Value
The object API is useful in code that is catching Return::Value objects.
my $return = Return::Value->new( bool => 0, string => "YOU FAIL", prop => { failed_objects => \@objects, }, );
Creates a new Return::Value object. You can set the following options.
bool, the boolean representation of the result. Defaults to false.
bool
errno, the error number. Defaults to 1 or 0 based on the value of bool.
errno
1
0
string, the string representation of the result.
string
data, data associated with the result, usually for success.
data
prop, properties assigned to the result.
prop
print "it worked" if $result->bool;
Returns a boolean describing the result as success or failure.
print "it worked" if $result->errno == 0;
Returns an errno for the result.
print $result->string unless $result->bool;
if ( $result->bool ) { my $data = $result->data; print foreach @{$data}; }
Returns the data structure passed to it.
printf "%s: %s', $result->string, join ' ', @{$result->prop('strings')} unless $result->bool;
Returns the return value's properties. Accepts the name of a property retured, or returns the properties hash reference if given no name.
Several operators are overloaded for Return::Value objects. They are listed here.
print "$result\n";
Stringifies to the string representation.
print $result unless $result;
Returns the bool representation.
Also returns the bool value.
To install Return::Value, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Return::Value
CPAN shell
perl -MCPAN -e shell install Return::Value
For more information on module installation, please visit the detailed CPAN module installation guide.