muba has asked for the wisdom of the Perl Monks concerning the following question:
According to perldoc perlop, terms have the highest precedence of everything:
A TERM has the highest precedence in Perl. They include variables, quote and quote-like operators, any expression in parentheses ...
Please consider the following code.
use 5.010; sub apple { say "apple" } sub banana { say "banana" } sub cherry { say "cherry" } apple && (banana || cherry)
Given that expressions in parentheses have topmost priority, one would expect Perl to evaluate (banana || cherry) first, which would boil down to say "banana" which would return 1, and only then to evaluate apple && (1).
So the output I more or less expected is:
banana apple
However, the output I get is:
apple banana
Please shed some light on this. Why isn't the parenthesed expression evaluated first? I understand the short circuiting nature of the logical operators — for one, this explains why cherry isn't outputted at all, but I fail to understand how && seems to have higher precedence than ( EXPR ), despite the information in the documentation.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Operator precedence
by BrowserUk (Patriarch) on Jan 12, 2013 at 23:13 UTC | |
by muba (Priest) on Jan 12, 2013 at 23:15 UTC | |
by BrowserUk (Patriarch) on Jan 12, 2013 at 23:35 UTC | |
by LanX (Saint) on Jan 13, 2013 at 01:10 UTC | |
by Tux (Canon) on Jan 20, 2013 at 13:51 UTC | |
by BrowserUk (Patriarch) on Jan 20, 2013 at 14:47 UTC | |
| |
Re: Operator precedence
by ikegami (Patriarch) on Jan 13, 2013 at 21:21 UTC | |
by muba (Priest) on Jan 18, 2013 at 22:43 UTC | |
Re: Operator precedence
by sundialsvc4 (Abbot) on Jan 14, 2013 at 03:08 UTC |