![]() |
![]()
| ![]() |
![]()
NAMEClass::ReturnValue - A return-value object that lets you treat it as as a boolean, array or object DESCRIPTIONClass::ReturnValue is a "clever" return value object
that can allow code calling your routine to expect:
EXAMPLEsub 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 paramters. 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 paramters. 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; METHODS
AUTHORJesse Vincent <jesse@bestpractical.com> BUGSThis module has, as yet, not been used in production code. I thing it should work, but have never benchmarked it. I have not yet used it extensively, though I do plan to in the not-too-distant future. If you have questions or comments, please write me. 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/ COPYRIGHTCopyright (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. SEE ALSOClass::ReturnValue isn't an exception handler. If it doesn't do what you want, you might want look at one of the exception handlers below: Error, Exception, Exceptions, Exceptions::Class You might also want to look at Contextual::Return, another implementation of the same concept as this module. POD ERRORSHey! The above document had some coding errors, which are explained below:
|