1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
NAME
Return::Value - Polymorphic Return Values
SYNOPSIS
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!
}
DESCRIPTION
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.
Functions
The functional interface is highly recommended for use within functions
that are using "Return::Value"s.
success
failure
Methods
The object API is useful in code that is catching "Return::Value"
objects.
new
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.
"errno", the error number. Defaults to 1 or 0 based on the value of
"bool".
"string", the string representation of the result.
"data", data associated with the result, usually for success.
"prop", properties assigned to the result.
bool
print "it worked" if $result->bool;
Returns a boolean describing the result as success or failure.
errno
print "it worked" if $result->errno == 0;
Returns an errno for the result.
string
print $result->string unless $result->bool;
Returns a boolean describing the result as success or failure.
data
if ( $result->bool ) {
my $data = $result->data;
print foreach @{$data};
}
Returns the data structure passed to it.
prop
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.
Overloading
Several operators are overloaded for "Return::Value" objects. They are
listed here.
Stringify
print "$result\n";
Stringifies to the "string" representation.
Boolean
print $result unless $result;
Returns the "bool" representation.
Numeric
Also returns the "bool" value.
|