Mailing List Archive

Perl question...
Hello!

Sorry for clean Perl question, although I use Apache::ASP. mod_perl is only
one my perl related mail list.

Simple example
---------------------
my $test1 = undef;
my $test2 = 2;
my $test3 = $test1 or $test2;
print "Value - $test3";
---------------------
returns
---------------------
Value -
---------------------
when
---------------------
my $test1 = undef;
my $test2 = 2;
my $test3 = $test1 || $test2;
print "Value - $test3";
---------------------
returns
---------------------
Value - 2
---------------------

We can read "Programming Perl" Chapter 2.5

2.5.20 Logical and, or, not, and xor

As more readable alternatives to &&, ||, and !, Perl provides the and, or
and not operators. The behavior of these operators _IS IDENTICAL_ - in
particular, they short-circuit the same way.

Why?
Where I can read more about it, if it is not a bug ?

PS. I can't imagine that it is a bug!
------------------------------------
Sergey Polyakov - Chief of WebZavod.
http://www.webzavod.ru
Re: Perl question... [ In reply to ]
> As more readable alternatives to &&, ||, and !, Perl provides the and, or
> and not operators. The behavior of these operators _IS IDENTICAL_ - in
> particular, they short-circuit the same way.

I guess their _behavior_ is identical but their _precedence_ is not the
same.

I think that

my $test3 = $test1 or $test2

is interpreted as

(my $test3 = $test1) or $test2

so you should try

my $test3 = ($test1 or $test2)

Victor.
Re: [OT] Perl question... [ In reply to ]
On Thu, Feb 24, 2000 at 05:31:57PM +0100, Victor Zamouline wrote:
> > As more readable alternatives to &&, ||, and !, Perl provides the and, or
> > and not operators. The behavior of these operators _IS IDENTICAL_ - in
> > particular, they short-circuit the same way.
>
> I guess their _behavior_ is identical but their _precedence_ is not the
> same.

Right. And those operators were not created as a more readable
alternative. They were created *because* their precedence is
different. The "and," "or," and "not" operators are supposed to be
lower in precendence so that you can say things like:

my $foo = $bar || $default or
die("Couldn't set foo");

Of course, this is just silly, and you should use parentheses!

-DeWitt
Re: [OT] Perl question... [ In reply to ]
>>>>> "DeWitt" == DeWitt Clinton <dewitt@eziba.com> writes:

DeWitt> Right. And those operators were not created as a more readable
DeWitt> alternative. They were created *because* their precedence is
DeWitt> different.

Well, the "xor" operator didn't have a trivial punctuation equivalent,
so there's actually added functionality. Yeah, you can do something like:

!(EXPRESSION_A) != !(EXPRESSION_B)

But that's just messy, when you can do this instead:

EXPRESSION_A xor EXPRESSION_B

DeWitt> Of course, this is just silly, and you should use parentheses!

If you want to use LISP, you know where to find it. :)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<merlyn@stonehenge.com> <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!