boolean(3pm) | User Contributed Perl Documentation | boolean(3pm) |
boolean - Boolean support for Perl
This document describes boolean version 0.46.
use boolean; do &always if true; do &never if false; do &maybe if boolean($value)->isTrue;
and:
use boolean ':all'; $guess = int(rand(2)) % 2 ? true : false; do &something if isTrue($guess); do &something_else if isFalse($guess);
Most programming languages have a native "Boolean" data type. Perl does not.
Perl has a simple and well known Truth System. The following scalar values are false:
$false1 = undef; $false2 = 0; $false3 = 0.0; $false4 = ''; $false5 = '0';
Every other scalar value is true.
This module provides basic Boolean support, by defining two special objects: "true" and "false".
When sharing data between programming languages, it is important to support the same group of basic types. In Perlish programming languages, these types include: Hash, Array, String, Number, Null and Boolean. Perl lacks native Boolean support.
Data interchange modules like YAML and JSON can now "use boolean" to encodedecoderoundtrip Boolean values.
This module defines the following functions:
Since true and false return objects, you can call methods on them.
By default this module exports the "true", "false" and "boolean" functions.
The module also defines these export tags:
This module offered an export tag, "-truth", that overrides the Perl interpreter's internal values for true and false. This has been found to corrupt the interpreter in some circumstances. Also, these overrides will no longer be possible as of Perl 5.22. Therefore, the "-truth" import tag is deprecated.
JSON::MaybeXS (or less preferably JSON.pm ) will encode Perl data with boolean.pm values correctly if you use the "convert_blessed" option:
use JSON::MaybeXS; use boolean -truth; my $json = JSON::MaybeXS->new->convert_blessed; say $json->encode({false => (0 == 1)}); # Says: '{"false":false}',
Ingy döt Net <ingy@cpan.org>
Copyright 2007-2016. Ingy döt Net.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <http://www.perl.com/perl/misc/Artistic.html>
2022-10-13 | perl v5.34.0 |