Why do Perl operators have different precedence than C operators?by faq_monk (Initiate)
|on Oct 08, 1999 at 00:27 UTC||Need Help??|
Current Perl documentation can be found at perldoc.perl.org.
Here is our local, out-dated (pre-5.6) version:
Actually, they don't. All C operators that Perl copies have the same precedence in Perl as they do in C. The problem is with operators that C doesn't have, especially functions that give a list context to everything on their right, eg print, chmod, exec, and so on. Such functions are called ``list operators'' and appear as such in the precedence table in the perlop manpage.
A common mistake is to write:
unlink $file || die "snafu";
This gets interpreted as:
unlink ($file || die "snafu");
To avoid this problem, either put in extra parentheses or use the super low
(unlink $file) || die "snafu"; unlink $file or die "snafu";
The ``English'' operators (
Another operator with surprising precedence is exponentiation. It binds
more tightly even than unary minus, making
Although it has the same precedence as in
($maybe ? $a : $b) = $x;