Class:...nValue(3pm)
β¨ NAME β¨
okay bestie, welcome to Class::ReturnValue β it's a Perl module (a 3pm manual page, user contributed, if you're keeping track of your man sections π
) and it's basically a return-value object that lets you treat it as a boolean, an array, or an object. three vibes, one little guy. iconic. π
π¦ DESCRIPTION π¦
so Class::ReturnValue is what the docs themselves call a "clever" return value object π and the whole point is that it lets the code calling your routine expect either:
- a boolean value (did it fail), or
- a list (what are the return values)
like, it just... flexes into whichever one the caller wanted. main character energy fr fr. π
πΈ EXAMPLE πΈ
here's the whole thing in action, no cap:
sub demo {
my $value = shift;
my $ret = Class::ReturnValue->new();
$ret->as_array('0', 'No results found');
unless($value) {
$ret->as_error(errno => '1',
message => "You didn't supply a parameter.",
do_backtrace => 1);
}
return($ret->return_value);
}
if (demo('foo')){
print "the routine succeeded with one parameter";
}
if (demo()) {
print "The routine succeeded with 0 parameters. shouldn't happen";
} else {
print "The routine failed with 0 parameters (as it should).";
}
my $return = demo();
if ($return) {
print "The routine succeeded with 0 parameters. shouldn't happen";
} else {
print "The routine failed with 0 parameters (as it should). ".
"Stack trace:\n".
$return->backtrace;
}
my @return3 = demo('foo');
print "The routine got ".join(',',@return3).
"when asking for demo's results as an array";
my $return2 = demo('foo');
unless ($return2) {
print "The routine failed with a parameter. shouldn't happen.".
"Stack trace:\n".
$return2->backtrace;
}
my @return2_array = @{$return2}; # TODO: does this work
my @return2_array2 = $return2->as_array;
(and yes, that # TODO: does this work comment is in the original and I am not touching it, it's part of the lore π)
π METHODS π
newβ instantiate a brand newClass::ReturnValueobject. baby's first return value π«Άas_arrayβ hand back the'as_array'attribute of this object, but as an actual array. slay.as_array [ARRAY]β if$selfgets called in an array context, it returns the array you specified in ARRAY. β¨as_error HASHβ turns this return-value object into an error return object. it takes three parameters:messagedo_backtraceerrno
okay let me break those down because there's lore:
'message'is a human readable error message explaining what's going on. like, words. for humans. π¬'do_backtrace'is a boolean. if it's true, a carp-style backtrace will be stored in$self->{'backtrace'}. it defaults to true.errnoandmessagedefault toundef. anderrno_must_ be specified. it's a numeric error number. any true integer value will cause the object to evaluate to false in a scalar context.
bestie. I know. "true value makes it false??" at first, this may look a bit counterintuitive, but it means you can have error codes and still allow simple use of your functions in a style like this:
if ($obj->do_something) {
print "Yay! it worked";
} else {
print "Sorry. there's been an error.";
}
as well as more complex use like this:
my $retval = $obj->do_something;
if ($retval) {
print "Yay. we did something\n";
my ($foo, $bar, $baz) = @{$retval};
my $human_readable_return = $retval;
} else {
if ($retval->errno == 20) {
die "Failed with error 20 (Not enough monkeys).";
} else {
die $retval->backtrace; # Die and print out a backtrace
}
}
errnoβ returns theerrnoif there's been an error. otherwise, returnundef. π€·βοΈerror_messageβ if there's been an error, return the error message. π£οΈbacktraceβ if there's been an error and we asked for a backtrace, return the backtrace. otherwise, returnundef.error_conditionβ if there's been an error, returnundef. otherwise return1. (yes it's the lil inverse guy, we love him π)
π AUTHOR π
Jesse Vincent <jesse@bestpractical.com> β shoutout to the author, absolute legend. π
π BUGS π
okay so real talk, the original docs say this module has, as yet, not been used in production code. the author says "I thing it should work" (that typo is verbatim and I'm keeping it, it's a whole mood π) but it has never been benchmarked. it also has not yet been used extensively, though there are plans to do so in the not-too-distant future. if you have questions or comments, please write.
if you need to report a bug, please send mail to <bug-class-returnvalue@rt.cpan.org> or report your error on the web at http://rt.cpan.org/
π COPYRIGHT π
Copyright (c) 2002,2003,2005,2007 Jesse Vincent <jesse@bestpractical.com>
you may use, modify, fold, spindle or mutilate this module under the same terms as perl itself. (fold. spindle. mutilate. the license said it's fine. π)
β SEE ALSO β
Class::ReturnValue isn't an exception handler. if it doesn't do what you want, you might want to look at one of the exception handlers below:
ErrorExceptionExceptionsExceptions::Class
you might also want to look at Contextual::Return, another implementation of the same concept as this module. π§
and that's the whole page, bestie!! this was perl v5.34.0, dated 2022-06-26, from the Class:...nValue(3pm) manual β go forth and return values in every format at once, you absolute icon πβ¨π
π¦ where this page comes from
- debian bookworm: libclass-returnvalue-perl 0.55-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libclass-returnvalue-perl 0.55-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libclass-returnvalue-perl 0.55-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libclass-returnvalue-perl 0.55-4 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libclass-returnvalue-perl 0.55-4 Β· 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