Attribute::Boolean - Mark scalars as pure booleans
Version v1.0.8
This allows you to flag a variable as a boolean. In numeric context, it will have the value 0 or 1. In string context is will have the value "false" or "true". In JSON, it will correctly return false or true values.
my $bool : Boolean; print $bool; # "false" $bool = (1 + 2 == 3); print $bool; # "true" print $bool ? "yes" : "no"; # "yes" $bool = false; print $bool ? "yes" : "no"; # "no"
This exports constants true and false.
An attribute can be declared boolean as follows:
my $bool : Boolean;
or
my $bool : Boolean = true;
If any perl true value is assigned, the variable is true; if a perl false value is assigned, the variable is false.
This returns 1 in numeric context, "true" in string context.
This returns 0 in numeric context, "false" in string context.
Provided that convert_blessed is set on the JSON (or JSON::XS) object, the variable will correctly convert to JSON true or false.
my $json = new JSON; $json->pretty->convert_blessed; my $bool : Boolean; my %hash = ( value => $bool, me => true, ); print $json->encode(\%hash); # { # "value" : false, # "me" : true # }
Cliff Stanford, <cpan@may.be>
<cpan@may.be>
Please report any bugs or feature requests to bug-attribute-boolean+ at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=attribute-boolean+. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-attribute-boolean+ at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Attribute::Boolean
Alan Haggai Alavi <alanhaggai@alanhaggai.org> for his Scalar::Boolean module which was the inspiration for this module.
<alanhaggai@alanhaggai.org>
Copyright 2014 Cliff Stanford.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Attribute::Boolean, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Attribute::Boolean
CPAN shell
perl -MCPAN -e shell install Attribute::Boolean
For more information on module installation, please visit the detailed CPAN module installation guide.