Return::DataButBool - Return a boolean value that also has arbitrary numeric and string values
This document describes Return::DataButBool version 0.0.3
use Return::DataButBool; sub whatever { ... return $ok ? data_but_true( $count ) : data_but_false( $count, $error ); }
later in the code using this function:
my $total = 0; for my $thing ( @stuff ) { my $rc = whatever( $thing ); $total += $rc; # numeric value if( !$rc ) { # boolean value carp "Error happened: $rc"; # string value } }
Perl's Zero-But-True ( 0e0 ) is a most handy tool (See String::ZeroButTrue). This module expands on that idea by having a return value that has different boolean, numeric, and string values.
For example you could return a count of files processed (say 42 which is "true") but still say it failed in boolean context.
or you could return any false value ( not just '0' ) but still return true. The flexibility allows for all sorts of use.
All 3 functions are exported by default.
The data_but_* functions take one or two arguments.
The first is the numeric value. Can be signed and/or decimal if you like. If its not [+-] digit [decimal digit] then it is passed through int() - see _get_num_from()
The second is the string value. If not specified the numeric value is used.
takes no args, returns good 'ol 0e0 (zero but true). See String::ZeroButTrue for more comprehensive alternatives.
The value returned evaluates to true in a boolean context. The numeric and string context are determined by the arguments passed as describe above.
The value returned evaluates to false in a boolean context. The numeric and string context are determined by the arguments passed as describe above.
This function makes sure your first argument is numeric ( possible sign, digits, possihle decimal point more digits - or int() )
Throws no errors or warnings
Return::DataButBool requires no configuration files or environment variables.
This uses the excellent Contextual::Return whcih can be used if your return needs are more complicated than this module's purpose.
None reported.
No bugs have been reported.
Please report any bugs or feature requests to bug-return-databutbool@rt.cpan.org, or through the web interface at http://rt.cpan.org.
bug-return-databutbool@rt.cpan.org
Daniel Muey <http://drmuey.com/cpan_contact.pl>
<http://drmuey.com/cpan_contact.pl>
Copyright (c) 2007, Daniel Muey <http://drmuey.com/cpan_contact.pl>. All rights reserved.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.
BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
To install Return::DataButBool, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Return::DataButBool
CPAN shell
perl -MCPAN -e shell install Return::DataButBool
For more information on module installation, please visit the detailed CPAN module installation guide.