Operator precedence

D

David Frauzel

I'm building a small lexical analyzer and using perlop / perlsyn /
perldata as a reference. (I'm not building the lexer in Perl, though.)

I've managed to get myself thoroughly confused with how operator
precedence works in Perl. At some point I got the (mis)conception that a
statement was evaluated from "inside out" based on the precedence of each
operator, but all of my tests seem to indicate that precedence means
little, in fact, in contrast to the typical left-to-right reading style
of human languages. Which makes sense. And doesn't.

Some of my tests:

$foo = 1; $foo or $foo++; print $foo;

The "or" operator has very low precedence, and ++ has very high. Yet the
second statement is plainly evaluated left to right - since $foo is true,
"or" is short-circuited, and $foo++ never happens.

1 or (print "foo");

Something similar happens here. Although parantheses and TERMs (the list
operator print, in this case) are documented as having the highest
precedence, the above statement proves once again that plain old left to
right is what's really happening.

Well, obviously precedence is still an issue in mathematical equations.
So that 2-3*4 is -10. But the same appears not to hold true with logical
operators:

0 and 1 && ++$foo; # $foo is not incremented

So what I'm left with wondering is: when *does* operator precedence
actually take effect? I have been so far unable to produce an example
where an operator somewhere inside of the statement with a higher
precedence actually operates before some operator to the left of it.
(Excepting statement modifiers, of course.)

Thanks for any insight.
 
D

David Frauzel

So what I'm left with wondering is: when *does* operator precedence
actually take effect? I have been so far unable to produce an example
where an operator somewhere inside of the statement with a higher
precedence actually operates before some operator to the left of it.
(Excepting statement modifiers, of course.)

Well, I came up with one:

print 0 < 0 || 1; # true
print 0 < 0 or 1; # false

But then how did Perl know to evaluate || before < in this statement, when
it (apparently?) does not know to evaluate ++ ...

0 and 1 && ++$foo;

.... in this statement?
 
L

Luc Van Hove

David Frauzel said:
Well, I came up with one:

print 0 < 0 || 1; # true
In this statement first "<" is executed, then "||", then "print".
print 0 < 0 or 1; # false
In this statement first "<" is executed, then "print", then "or".
The right operant of "or" is not executed because the left operant is false.
But then how did Perl know to evaluate || before < in this statement, when
it (apparently?) does not know to evaluate ++ ...

0 and 1 && ++$foo;

Binary "and" performs a short-circuit logical AND operation. That is, if the
left operand is false, the right operand is not even evaluated.

This is explained in: http://www.perldoc.com/perl5.6/pod/perlop.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top