Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Booleans in Perl

Reply
Thread Tools

Booleans in Perl

 
 
David Williams
Guest
Posts: n/a
 
      04-20-2007
Small question.
Why does PERL do the following

$a=FALSE;
$b=FALSE;
$c=($a && $b);
if($c){
echo "the expr evaluates to true";
}
else{
echo "the expr evaluates to false;
}

I always get "the expr evaluates to true";

if it is false && false, should it not be false?
My workaround is to use the eq operand
--
David Williams
Georgia Institute of Technology, Atlanta Georgia, 30332
Email: http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Charlton Wilbur
Guest
Posts: n/a
 
      04-20-2007
>>>>> "DW" == David Williams <(E-Mail Removed)> writes:

DW> Small question. Why does PERL do the following

DW> $a=FALSE; $b=FALSE; $c=($a && $b); if($c){ echo "the expr
DW> evaluates to true"; } else{ echo "the expr evaluates to false;
DW> }

DW> I always get "the expr evaluates to true";

use strict; use warnings;

You'll probably find out that that code doesn't do what you think it does.

(That is, if you're using Perl at all; the 'echo' function in there
makes me think you're using PHP, in which case you're in the wrong
newsgroup.)

DW> if it is false && false, should it not be false?

It's not false && false; it's the string "FALSE" and the string
"FALSE" - since non-empty strings evaluate to true, the whole
expression evaluates to true.

This will all be made clearer when you enable strictures and warnings.

Charlton


--
Charlton Wilbur
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      04-20-2007
David Williams wrote:
> Small question.
> Why does PERL do the following
>
> $a=FALSE;
> $b=FALSE;
> $c=($a && $b);
> if($c){
> echo "the expr evaluates to true";
> }
> else{
> echo "the expr evaluates to false;
> }
>
> I always get "the expr evaluates to true";
>
> if it is false && false, should it not be false?
> My workaround is to use the eq operand


No, use some small code extension as shown and
proceed as you started:
==>

use strict;
use warnings;

package php;
sub echo { print @_ }

package bool;
sub true { return 1 }
sub false { return 0 }
use constant TRUE => true;
use constant FALSE => false;


package main; # <== your program starts here

my $a1 = bool::TRUE;
my $b1 = bool::FALSE;
my $c = ($a1 && $b1);

if( $c ){
php::echo "the expr evaluates to true"
}
else{
php::echo "the expr evaluates to false"
}

<==

I guess, perl6 will have the bool included.

Regards

M.


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      04-20-2007
Mirco Wahab schreef:

> sub true { return 1 }
> sub false { return 0 }
> use constant TRUE => true;
> use constant FALSE => false;


That looks like sub-stacking, but the compiler optimizes it:

$ perl -MO=Deparse -e'
sub true { return 1 }
sub false { return 0 }
use constant TRUE => true;
use constant FALSE => false;
$x = TRUE;
$y = FALSE
'
sub true {
return 1;
}
sub false {
return 0;
}
use constant ('TRUE', true());
use constant ('FALSE', false());
$x = 1;
$y = 0;
-e syntax OK



I write such more like:

$ perl -MO=Deparse -e'
use constant {FALSE => 0, TRUE => !0};
$x = TRUE;
$y = FALSE
'
use constant ({'FALSE', 0, 'TRUE', 1});
$x = 1;
$y = 0;
-e syntax OK

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      04-20-2007
>>>>> "Mirco" == Mirco Wahab <(E-Mail Removed)> writes:

Mirco> package bool;
Mirco> sub true { return 1 }
Mirco> sub false { return 0 }
Mirco> use constant TRUE => true;
Mirco> use constant FALSE => false;

This is almost never a good idea.

*Somebody* out there will write:

if (some_func() == TRUE) { ... }

and then get burned when some_func() returns 42 for true, not your
precious "1".

Remember, in Perl, there are many values of true and fewer but still many
values of false. Stop trying to force Perl's truth model into
a simple boolean space.

print "Just another Perl hacker,"; # the original
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL: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!

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      04-20-2007
David Williams <(E-Mail Removed)> wrote:
> Small question.
> Why does PERL do the following
>
> $a=FALSE;
> $b=FALSE;
> $c=($a && $b);
> if($c){
> echo "the expr evaluates to true";
> }
> else{
> echo "the expr evaluates to false;
> }
>
> I always get "the expr evaluates to true";



Because the string 'FALSE' is a true value.

It is a true value because it is not one of the four values that
Perl (not PERL) considers false:

1) the number zero
2) the empty string
3) the undef value
4) a one character string where the character is the zero digit

> if it is false && false, should it not be false?



FALSE && FALSE _is_ FALSE (which is a true value):

-------------------------------
#!/usr/bin/perl
use warnings;
#use strict;

my $a=FALSE;
my $b=FALSE;
my $c=($a && $b);

print "\$c is '$c'\n";
-------------------------------


--
Tad McClellan SGML consulting
(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      04-20-2007
Randal L. Schwartz wrote:
>>>>>> "Mirco" == Mirco Wahab <(E-Mail Removed)> writes:

>
> Mirco> package bool;
> Mirco> sub true { return 1 }
> Mirco> sub false { return 0 }
> Mirco> use constant TRUE => true;
> Mirco> use constant FALSE => false;
>
> This is almost never a good idea.
>
> *Somebody* out there will write:
>
> if (some_func() == TRUE) { ... }
>
> and then get burned when some_func() returns 42 for true, not your
> precious "1".


Oh, I forgot! You are, of course, right. I was blind-
folded, because in php, the following *would* work:

<going into php mode>

$c = 42;

if($c == TRUE) {
echo "$c is TRUE\n";
}
else {
echo "$c is FALSE\n";
}
</going into php mode>

and its TRUE/FALSE would be a magical
function to the left hand side.

What would be an appropriate idiomatic
approximation in Perl aside from the
obvious

if( $c ) {
print "$c is TRUE\n";
}

(or - why would't be any?)


Regards & Thanks

Mirco
 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      04-20-2007
Dr.Ruud wrote:
> I write such more like:
>
> $ perl -MO=Deparse -e'
> use constant {FALSE => 0, TRUE => !0};
> $x = TRUE;
> $y = FALSE
> '
> use constant ({'FALSE', 0, 'TRUE', 1});
> $x = 1;
> $y = 0;
> -e syntax OK


OK, I tried to conjure up an overly compli-
cated 'php compatibility framework'

Yours is much more adequate, but the !0 would
(imho) always evaluate to 1 in Perl, so why
wouldn't one write it.


Regards & Thanks

Mirco
 
Reply With Quote
 
Randal L. Schwartz
Guest
Posts: n/a
 
      04-20-2007
>>>>> "Mirco" == Mirco Wahab <(E-Mail Removed)> writes:

Mirco> Yours is much more adequate, but the !0 would (imho) always evaluate to
Mirco> 1 in Perl, so why wouldn't one write it.

There's actually no promise of that. "Boolean-returning" operations are free
to return "any true" for true and "any false" for false, although in practice,
they return a predefined scalar (internally) of "true" and "false", which
look like "1" and "" respectively. Yes, "false" is not 0, but "".

print "Just another Perl hacker,"; # the original

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<(E-Mail Removed)> <URL: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!

--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      04-20-2007
David Williams wrote:
> Small question.
> Why does PERL do the following
>
> $a=FALSE;
> $b=FALSE;
> $c=($a && $b);
> if($c){
> echo "the expr evaluates to true";
> }
> else{
> echo "the expr evaluates to false;
> }
>
> I always get "the expr evaluates to true";


Interesting result. I am getting a bunch or compiler errors instead:

String found where operator expected at C:\tmp\t.pl line 6, near "echo "the
expr
evaluates to true""
(Do you need to predeclare echo?)
String found where operator expected at C:\tmp\t.pl line 9, at end of line
(Missing semicolon on previous line?)
syntax error at C:\tmp\t.pl line 6, near "echo "the expr evaluates to true""
Can't find string terminator '"' anywhere before EOF at C:\tmp\t.pl line 9.

Which version of Perl are you using?

jue




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
array of booleans zeus2@hotmail.com Java 4 10-01-2005 05:29 PM
g++: integers as booleans, no warning? Martin Herbert Dietze C++ 9 02-18-2005 09:03 AM
Re: Booleans and comparison results Gerrit Holl Python 1 06-25-2003 03:19 AM
Re: Booleans and comparison results =?ISO-8859-1?Q?Gerhard_H=E4ring?= Python 0 06-24-2003 03:49 PM
Booleans and comparison results Roman Suzi Python 0 06-24-2003 03:19 PM



Advertisments