ReturnValue - A structured return value for failure or success
use ReturnValue; sub do_something { ...; return ReturnValue->error( value => $your_usual_error_value, description => 'Some longer description', tag => 'short_value' ) if $failed; return ReturnValue->success( value => $your_usual_return_value, description => 'Some longer description', tag => 'short_value' ) unless $failed; } my $result = do_something(); if( $result->is_error ) { ...; # do error stuff } my $result = do_something_else(); for( $result->tag ) { when( 'tag1' ) { ... } when( 'tag2' ) { ... } }
The ReturnValue class provides a very simple wrapper around a value so you can tell if it's a success or failure without taking pains to examine the particular value. Instead of using exceptions, you inspect the class of the object you get back. Errors and successes flow through the same path.
ReturnValue
This isn't particularly interesting for success values, but can be helpful with multiple ways to describe an error.
Create a success object
Create an error object
The value that you'd normally return. This class doesn't care what it is. It can be a number, string, or reference. It's up to your application to figure out how you want to do that.
A long description of the return values,
A short tag suitable for switching on in a given, or something similar.
given
Returns the class for success objects
Returns the class for error objects
Returns true is the result represents a success
Returns true is the result represents an error
This source is in Github:
http://github.com/perlreview/returnvalue/
brian d foy, <bdfoy@cpan.org>
Copyright © 2013-2022, brian d foy <bdfoy@cpan.org>. All rights reserved.
You may redistribute this under the terms of the Artistic License 2.0.
To install ReturnValue, copy and paste the appropriate command in to your terminal.
cpanm
cpanm ReturnValue
CPAN shell
perl -MCPAN -e shell install ReturnValue
For more information on module installation, please visit the detailed CPAN module installation guide.