Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Mac > Perl on OSX > Re: Is there a ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 11 of 11 Topic 1001 of 1075
Post > Topic >>

Re: Is there a True Boolean type in Perl?

by david@[EMAIL PROTECTED] (David Cantrell) Oct 16, 2007 at 04:19 PM

On Mon, Oct 15, 2007 at 01:27:26PM -0700, 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 place to discuss this is the perl5-****ters mailing list, not an
obscure platform-specific mailing list.

And the answer is no.  Because we already have a real Boolean.  We have
boolean *context*.  An operator like ?: forces its first operand to be
evaluated in boolean context:

    $value ? 'true' : 'false'

That first operand can, of course, be a complex expression, the end
result of which is evaluated in boolean context:

    cheezburger->{$cutecat}->() ? print "LOL!" : print "OH NOES!"

We also don't have int or float or a numeric type (the internals do, but
Perl the language doesn't) - we have numeric context.  Consider this:

    $ perl -e 'print "foo\n" if "2abc" == "2def"'

The == operator forces both its operands to be evaulated in numeric
context before comparing them.  As numbers, 2abc and 2def are both just
plain ol' 2, so the == returns true and we print.  If we change it to
this:

    $ perl -e 'print "foo\n" if "2abc" eq "2def"'

then the eq operator evaluates its operands in string context, where
2abc and 2def most definitely aren't the same.

> The "C" programmers want me to use "0"'s and "1"'s.

Seems reasonable.  Those do evaluate to false and true in perl.  Perl's
comparison operators return 1 for true and the empty string for false.
In numeric context that's the same as returning 1 and 0 because:

    '' == 0

-- 
David Cantrell | Hero of the Information Age

   When a man is tired of London, he is tired of life
      -- Samuel Johnson
 




 11 Posts in Topic:
Is there a True Boolean type in Perl?
mbarto@[EMAIL PROTECTED]   2007-10-15 13:27:26 
Re: Is there a True Boolean type in Perl?
chas.owens@[EMAIL PROTECT  2007-10-15 16:54:08 
Re: Is there a True Boolean type in Perl?
merlyn@[EMAIL PROTECTED]   2007-10-16 12:25:29 
Re: Is there a True Boolean type in Perl?
gblackburn@[EMAIL PROTECT  2007-10-15 17:25:23 
Re: Is there a True Boolean type in Perl?
jeremiah@[EMAIL PROTECTED  2007-10-15 23:59:55 
Re: Is there a True Boolean type in Perl?
mbarto@[EMAIL PROTECTED]   2007-10-15 18:20:22 
Re: Is there a True Boolean type in Perl?
douglist@[EMAIL PROTECTED  2007-10-15 19:40:19 
Re: Is there a True Boolean type in Perl?
trey@[EMAIL PROTECTED] (  2007-10-17 12:40:08 
Re: Is there a True Boolean type in Perl?
chas.owens@[EMAIL PROTECT  2007-10-16 05:55:15 
Re: Is there a True Boolean type in Perl?
chas.owens@[EMAIL PROTECT  2007-10-16 06:01:02 
Re: Is there a True Boolean type in Perl?
david@[EMAIL PROTECTED]   2007-10-16 16:19:28 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Oct 12 3:25:14 CDT 2008.