Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Why do Perl operators have different precedence than C operators?

by faq_monk (Initiate)
on Oct 08, 1999 at 00:27 UTC ( #685=perlfaq nodetype: print w/replies, xml ) Need Help??

Current Perl documentation can be found at

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 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 product 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;

Log In?

What's my password?
Create A New User
[Corion]: Weird - this "Install our backdoor SDK" spam is persistent. And I get the feeling it's not even automated, as it seems to be a re-forward of each mail, maybe even manually. Ah well, a quick rule in Thunderbird will move them to Spam automatically now :D
[Corion]: (the "backdoor SDK" is for distributing my software with an included backdoor, not for backdooring my own machine)
[marto]: ask for a trial
[marto]: then trick them into running your app
[marto]: once you have access, remove your email address from their system :P
[shmem]: why just that email address? why filtering?
[Corion]: shmem: I use email for contact, so I'd still like to receive emails from other people. Hence, I filter my email.
[Corion]: marto: Sneaky :) But I think I don't want to even give them the impression that anybody reads their mail :)

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2018-05-25 14:05 GMT
Find Nodes?
    Voting Booth?