NAME
Object::Boolean - Represent boolean values as objects
SYNOPSIS
use
Object::Boolean;
True
=> {
-as
=>
'TRUE'
},
False
=> {
-as
=>
'FALSE'
};
# Create a "false" object by calling new() with a Perl
# false value or the word 'false'.
my
$f
= Object::Boolean->new(0);
my
$f
= Object::Boolean->new(
''
);
my
$f
= Object::Boolean->new(2+2==3);
my
$f
= Object::Boolean->new(
'false'
);
# Create a "true" object by calling new() with anything else
my
$t
= Object::Boolean->new(1);
my
$t
= Object::Boolean->new(2+2==4);
my
$t
= Object::Boolean->new(
'true'
);
my
$t
= Object::Boolean->new(
'elephant'
);
# In boolean context, it behaves like a boolean value.
if
(
$f
) {
"it's true"
}
"\$f is false"
unless
$f
;
"1+1==2 and it's true"
if
1+1==2 &&
$t
;
# In string context, it becomes "true" or "false"
"It was a $f alarm."
;
"Those are $f teeth."
;
# Boolean negation produces a new boolean object.
(!
$f
).
" love is hard to find."
;
# Checking for numeric or string equality with other boolean
# objects compares them as though they were in a boolean context.
if
(
$t
!=
$f
) {
"They are not the same."
}
# like xor
if
(
$t
==
$t
) {
"They are both true or both false."
}
if
(Object::Boolean->new(1) eq Object::Boolean->new(2)) {
# this will be true
}
# Comparison to non-boolean objects treats booleans as strings
# for string equality or the numbers 0 or 1 for numeric equality
my
$true
= Object::Boolean->new(
'true'
);
"true"
if
$true
eq
'true'
;
# true
'true'
if
$true
== 1;
# also true
'true'
if
$true
== 27;
# no, not true
'true'
if
$true
eq
'True'
;
# not true
DESCRIPTION
Package for representing booleans as objects which stringify to true/false and numerify to 0/1. Derived classes can easily stringify/numerify to other values.
FUNCTIONS
SEE ALSO
Object::Boolean::YesNo -- to stringify to 'Yes' and 'No' instead of 'true' and 'false'.
VERSION
Version 0.02
AUTHOR
Brian Duggan, <bduggan at matatu.org>
LICENSE
Copyright 2008 Brian Duggan, all rights reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.