Is there a True Boolean type in Perl?

2007-10-15 Thread Michael Barto




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.
-- 





  

  
  


  Michael Barto
  Software Architect
  
  
  
  


   LogiQwest
Inc.
16458 Bolsa Chica Street, # 15
Huntington Beach, CA  92649
  http://www.logiqwest.com/
  
  
     
  [EMAIL PROTECTED]
Tel:  714 377 3705
Fax: 714 840 3937
Cell: 714 883 1949
  
  


  'tis a gift to be
simple
   


   This e-mail may contain
LogiQwest
proprietary information and should be treated as confidential. 

  








Re: Is there a True Boolean type in Perl?

2007-10-15 Thread Chas. Owens
On 10/15/07, Michael Barto <[EMAIL PROTECTED]> 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.
snip

Perl 5 does not have a boolean type.  Perl considers the following
things as false: any number that is equivalent to 0 (0.0, 0e0, etc.),
the string '0', the empty string, undef, or an empty list ( i.e. ()).
Anything else is considered true.  Note that the string "0e0" which
evaluates in a numeric context to 0 is not false, but the number 0e0
is false.  In general the values 1 and 0 are used just like they are
in C.

#!/usr/bin/perl

use strict;

my @a = (undef, 0, 0.0, 0e0, '', "0e0", "0 but true", 1, "foo");

print "() is ", ( () ? 'true' : 'false' ), "\n";
for my $val (@a) {
print "[$val] is ", ($val ? 'true' : 'false'), "\n";
}


Re: Is there a True Boolean type in Perl?

2007-10-15 Thread Gary Blackburn


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)


---
Gary Blackburn
[EMAIL PROTECTED]




Re: Is there a True Boolean type in Perl?

2007-10-15 Thread jeremiah


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


Re: Is there a True Boolean type in Perl?

2007-10-15 Thread Michael Barto




I think in the more newer languages, they have implemented true
booleans. Perl is kind of old school. Pascal defines them as a
grandfather of languages. Therefore as one migrates the languages to a
higher levels (e.g. Perl[n]), they all will end up with a boolean data
type. Therefore, I think the $true and $false is a more consistent
method in a multiple languages environments (particularly doing web
things [Java and _javascript_] for a consistent set of rules across the
board. Thanks for your input.

[EMAIL PROTECTED] wrote:

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
  
  


-- 





  

  
  


  Michael Barto
  Software Architect
  
  
  
  


   LogiQwest
Inc.
16458 Bolsa Chica Street, # 15
Huntington Beach, CA  92649
  http://www.logiqwest.com/
  
  
     
  [EMAIL PROTECTED]
Tel:  714 377 3705
Fax: 714 840 3937
Cell: 714 883 1949
  
  


  'tis a gift to be
simple
   


   This e-mail may contain
LogiQwest
proprietary information and should be treated as confidential. 

  








Re: Is there a True Boolean type in Perl?

2007-10-15 Thread Doug McNutt
At 18:20 -0700 10/15/07, Michael Barto wrote:
>I think in the more newer languages, they have implemented true booleans. Perl 
>is kind of old school.

use constant TRUE => 1;
use constant FALSE => 0;

Is a complete solution in perl 5.  There was once a reason back in the time of 
grandfathered languages for booleans which actually used only one bit of a 
precious word of memory. That would be a "true" boolean. I'll bet those "new 
school" languages use a native word, 16 , 32, or perhaps 64 bits while arguing 
that it's faster and better that way.
-- 

-->  Halloween  == Oct 31 == Dec 25 == Christmas  <--


Re: Is there a True Boolean type in Perl?

2007-10-16 Thread Chas. Owens
On 10/15/07, Michael Barto <[EMAIL PROTECTED]> wrote:
>
>  I think in the more newer languages, they have implemented true booleans.
>  Perl is kind of old school. Pascal defines them as a grandfather of 
> languages.
>  Therefore as one migrates the languages to a higher levels (e.g. Perl[n]), 
> they
>  all will end up with a boolean data type. Therefore, I think the $true and 
> $false
>  is a more consistent method in a multiple languages environments 
> (particularly
>  doing web things [Java and Javascript] for a consistent set of rules across 
> the
>  board. Thanks for your input.
snip

Perl 6 will have true boolean values: True and False.  See S02 for
more information.

http://dev.perl.org/perl6/doc/design/syn/S02.html


Re: Is there a True Boolean type in Perl?

2007-10-16 Thread Chas. Owens
On 10/15/07, Doug McNutt <[EMAIL PROTECTED]> wrote:
> At 18:20 -0700 10/15/07, Michael Barto wrote:
> >I think in the more newer languages, they have implemented true booleans. 
> >Perl
> >is kind of old school.
>
> use constant TRUE => 1;
> use constant FALSE => 0;
>
> Is a complete solution in perl 5.  There was once a reason back in the time of
> grandfathered languages for booleans which actually used only one bit of a
> precious word of memory. That would be a "true" boolean. I'll bet those "new 
> school"
> languages use a native word, 16 , 32, or perhaps 64 bits while arguing that 
> it's faster
> and better that way.

It is actually worse than you suspect.  Most of them implement boolean
types as objects (which take up more than one word).  On the plus side
they tend to be singleton objects, but the reference still takes up
(at least) a word.


Re: Is there a True Boolean type in Perl?

2007-10-16 Thread David Cantrell
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-porters 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


Re: Is there a True Boolean type in Perl?

2007-10-16 Thread Randal L. Schwartz
> ""Chas" == "Chas Owens" <[EMAIL PROTECTED]> writes:

"Chas> On 10/15/07, Michael Barto <[EMAIL PROTECTED]> 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.
"Chas> snip

"Chas> Perl 5 does not have a boolean type.  Perl considers the following
"Chas> things as false: any number that is equivalent to 0 (0.0, 0e0, etc.),
"Chas> the string '0', the empty string, undef, or an empty list ( i.e. ()).

"The empty list" doesn't belong in that list.  Boolean forces scalar context,
and the scalar version of () is simply undef, which you've already listed.
Unless you also want to add (undef) or (0) or ("") to your list as well, which
are just as false, and for the same reason. :)

Yes, I know the third edition of the camel added this, but they were wrong.
Please don't propogate the mistake.  No edition of the camel that I was in
charge of got this wrong. :)

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


Re: Is there a True Boolean type in Perl?

2007-10-17 Thread Trey Harris

In a message dated Mon, 15 Oct 2007, Doug McNutt writes:


At 18:20 -0700 10/15/07, Michael Barto wrote:

I think in the more newer languages, they have implemented true booleans. Perl 
is kind of old school.


use constant TRUE => 1;
use constant FALSE => 0;

Is a complete solution in perl 5.


Um, no, it's not:

  #!/usr/bin/perl

  use strict;
  use warnings;

  use constant TRUE => 1;
  use constant FALSE => 0;

  # Return a list of the sequence of even numbers from 0..$num
  # or false if we got an odd number
  sub evens_up_to {
  my $num = shift;

  if ($num % 2) {
  # didn't get an even!
  return FALSE;
  }

  return grep { not $_ % 2 } 0..$num;
  }

  for my $n (2, 4, 5) {
  if (my @nums = evens_up_to($n)) {
  print "$n is even: @nums\n";
  }
  else {
  print "$n is not even.\n";
  }
  }

Output is:
  2 is even: 0 2
  4 is even: 0 2 4
  5 is even: 0

Oops.  Now, change the return line:

  --- true.pl 2007-10-17 12:26:28.0 -0400
  +++ truenew.pl  2007-10-17 12:27:20.0 -0400
  @@ -13,5 +13,5 @@
 if ($num % 2) {
 # didn't get an even!
  -  return FALSE;
  +  return; # false via bare return
 }

Now you get:
  2 is even: 0 2
  4 is even: 0 2 4
  5 is not even.

Without resorting to tied handles or objects or other heavyweight 
chicanery, there is no single "FALSE" value you can define that will be 
false in all cases.  Better to use bare return when it may be used like 
the above.


On the other hand, when a subroutine returns a scalar boolean, you 
probably *do* want to return 0 or undef, otherwise you can get into this 
nasty situation:


  my ($ready1, $ready2) = ($data1->is_ready, $data2->is_ready);

If is_ready() uses bare "return" fore false, then in the case where $data1 
is not ready but $data2 is, $ready1 would be set to 1 and $ready2 would be 
undef, the exact opposite of what you'd expect, because $data1->is_ready 
would return the empty list so $data2->is_ready would squeeze over and 
fill the $ready1 spot.  Nasty.


Moral of the story: there is no single false value in Perl 5, and trying 
to pretend there is one that is valid in all situations is asking for 
trouble.


As others have already noted, Perl 6 fixes this by both having a primitive 
boolean type and by having True and False roles that any value can mix in 
(so you can have a "0 but True" value or a "1 but False" one for example, 
or a false list containing elements, or a true list that's empty).


Trey