On Oct 15, 2007, at 11:25 PM, Gary Blackburn wrote:


On Oct 15, 2007, at 4:27 PM, Michael Barto wrote:

As both Java and Javascript both have a 'true' and 'false' or Boolean data type, is there any interest in evolution of Perl to have a true Boolean. Or what is the preferred method to do this in Perl. The "C" programmers want me to use "0"'s and "1"'s.

In my experience the most common convention is to use undef and 1 as your boolean values, as in:

my $is_scared; # is_scared is initially set to undef, which evaluates as "false"

if ($monsters_under_my_bed) {
        $is_scared = 1;
}

Yeah, it's probably bad karma to use "undef" when you mean "0" but this approach is very perl-ish, easy to read, and even recommended by Damian Conway in his "Perl Best Practices" book (page 40... just checked. :-D)

You can, if you really want to, do this:

my $true = 1;
my $false;

Still, it is not much of a substitute for knowing what exactly is _inside_ your variable and testing it to make sure. Plus, boolean values are un-perlish. Look at this for example:

if ($var) {
   print "Yep!";
} else {
   print "Nep!";
}

That is perlish, testing if $var is defined and doing some thing based on that. You can of course make it clearer with

if ( defined ($var))

But to be really perly and terse, this is the idiom:

print "Yep" if ($var);

        Jeremiah

Reply via email to