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