String::BooleanSimple - Gets the boolean representative of a string
# imports all functions use String::BooleanSimple ':all'; # imports only is_true() use String::BooleanSimple qw(is_true);
Some static functions returning a boolean value (0 or 1) of a string like 'true', 'false', etc. In the background it returns the value from the module boolean, which holds a true/false method. What means, if you change that rules, it would not return a 0 or 1, but whatever you defined.
boolean
Exporter
my $boolean = boolean($string, $defaultstring);
Returns 1 if the string matches to a postive pattern like "true". Returns 0 if the string matches to a negative pattern like "false".
my $boolean = isFalse($string, $defaultstring);
If you like the java style, you may import that alias
my $boolean = isTrue($string, $defaultstring);
my $boolean = is_false($string, $defaultstring);
Returns 1 if the string matches to a negative pattern like "false".
my $boolean = is_true($string, $defaultstring);
Returns 1 if the string matches to a positive pattern like "true".
Supports these strings:
true yes active enabled on y ok positive 1 2 3 4 5 6 7 8 9 false no inactive disabled off n not ok negative 0
If the string does not match, it causes an error. Whitespace at the beginning or end will be automatically removed.
You may set a default string as second parameter on the functions. It works if the first value can not be indentified as valid string. An empty string is also a non-valid string and will trigger the default value.
Example:
if ( is_true("","false") ){};
it will be false. Typical for reading config files, where a value does not allways exist.
If the default value as well is not a valid string, it dies with an error.
If the calling application using the module boolean, you may write it like that:
use boolean ':all'; use String::BooleanSimple 'boolean'; my $s='positive' if ( isTrue( boolean($s) ) ){...}; if ( boolean($s) == true ){...};
Please note, here the isTrue() function is part of "use boolean"! It is not imported with ':all' from String::BooleanSimple because that is a conflict.
The following example is possible and logical, but looks silly:
if ( is_true($s) == true ){...};
Theoretically you must do like that, if "use boolean", because who knows what is realy "true" and "false"? But it is not very nice to read it and the simple way "if( is_true() )" worth to risk that maybe false is not '0'.
In other words, if the calling app is not using perl's "boolean" but somehow in perl's module "boolean" the value of true/false does not fit anymore to 1/0, it might be, that using is_true/is_false with String::BooleanSimple will cause wrong behaviour.
Andreas Hernitscheck ahernit(AT)cpan.org
You can redistribute it and/or modify it under the conditions of LGPL.
To install String::BooleanSimple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm String::BooleanSimple
CPAN shell
perl -MCPAN -e shell install String::BooleanSimple
For more information on module installation, please visit the detailed CPAN module installation guide.