| boolean documentation | Contained in the boolean distribution. |
boolean - Boolean support for Perl
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
encode/decode/roundtrip Boolean values.
This module defines the following functions:
This function returns a scalar value which will evaluate to true. The value is a singleton object, meaning there is only one "true" value in a Perl process at any time. You can check to see whether the value is the "true" object with the isTrue function described below.
This function returns a scalar value which will evaluate to false. The value is a singleton object, meaning there is only one "false" value in a Perl process at any time. You can check to see whether the value is the "false" object with the isFalse function described below.
Casts the scalar value to a boolean value. If $scalar is true, it
returns boolean::true, otherwise it returns boolean::false.
Returns boolean::true if the scalar passed to it is the
boolean::true object. Returns boolean::false otherwise.
Returns boolean::true if the scalar passed to it is the
boolean::false object. Returns boolean::false otherwise.
Returns boolean::true if the scalar passed to it is the
boolean::true or boolean::false object. Returns boolean::false
otherwise.
Since true and false return objects, you can call methods on them.
Same as isTrue($boolean).
Same as isFalse($boolean).
By default this module exports the true, false and boolean functions.
The module also defines these export tags:
Exports true, false, boolean, isTrue, isFalse, isBoolean
Ingy döt Net <ingy@cpan.org>
Copyright (c) 2007, 2008, 2010, 2011. 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
| boolean documentation | Contained in the boolean distribution. |
package boolean; use 5.005003; use strict; # use warnings; $boolean::VERSION = '0.27'; my ($true, $false); use overload '""' => sub { ${$_[0]} }, '!' => sub { ${$_[0]} ? $false : $true }, fallback => 1; use base 'Exporter'; @boolean::EXPORT = qw(true false boolean); @boolean::EXPORT_OK = qw(isTrue isFalse isBoolean); %boolean::EXPORT_TAGS = ( all => [@boolean::EXPORT, @boolean::EXPORT_OK], test => [qw(isTrue isFalse isBoolean)], ); my ($true_val, $false_val, $bool_vals); BEGIN { my $have_readonly = eval { require Readonly }; my $t = 1; my $f = 0; $true = do {bless \$t, 'boolean'}; $false = do {bless \$f, 'boolean'}; if ( $have_readonly ) { Readonly::Scalar($t => $t); Readonly::Scalar($f => $f); } $true_val = overload::StrVal($true); $false_val = overload::StrVal($false); $bool_vals = {$true_val => 1, $false_val => 1}; } sub true() { $true } sub false() { $false } sub boolean($) { die "Not enough arguments for boolean::boolean" if scalar(@_) == 0; die "Too many arguments for boolean::boolean" if scalar(@_) > 1; return not(defined $_[0]) ? false : "$_[0]" ? $true : $false; } sub isTrue($) { not(defined $_[0]) ? false : (overload::StrVal($_[0]) eq $true_val) ? true : false; } sub isFalse($) { not(defined $_[0]) ? false : (overload::StrVal($_[0]) eq $false_val) ? true : false; } sub isBoolean($) { not(defined $_[0]) ? false : (exists $bool_vals->{overload::StrVal($_[0])}) ? true : false; } 1;