The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Object::Boolean - Represent boolean values as objects

SYNOPSIS

    use Object::Boolean;
    use Object::Boolean qw/True False/; # ..or export some constants
    use Object::Boolean                 # ..or rename those constants
        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) { print "it's true" }
    print "\$f is false" unless $f;
    print "1+1==2 and it's true" if 1+1==2 && $t;

    # In string context, it becomes "true" or "false"
    print "It was a $f alarm.";
    print "Those are $f teeth.";

    # Boolean negation produces a new boolean object.
    print (!$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) { print "They are not the same." } # like xor
    if ($t==$t) { print "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');
    print "true" if $true eq 'true'; # true
    print 'true' if $true == 1;      # also true
    print 'true' if $true == 27;     # no, not true
    print '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

new

Create a new Object::Boolean object.

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.