From: PerlFAQ Server on
This is an excerpt from the latest version perlfaq7.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at .


7.7: Why do Perl operators have different precedence than C operators?

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

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 precedence "or" operator:

(unlink $file) || die "snafu";
unlink $file or die "snafu";

The "English" operators ("and", "or", "xor", and "not") deliberately
have precedence lower than that of list operators for just such
situations as the one above.

Another operator with surprising precedence is exponentiation. It binds
more tightly even than unary minus, making "-2**2" produce a negative
not a positive four. It is also right-associating, meaning that
"2**3**2" is two raised to the ninth power, not eight squared.

Although it has the same precedence as in C, Perl's "?:" operator
produces an lvalue. This assigns $x to either $a or $b, depending on the
trueness of $maybe:

($maybe ? $a : $b) = $x;


The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in