Terms and List Operators (Leftward)
Any term is of highest precedence in Perl. Terms include variables, quote and quote-like operators, most expressions in parentheses, brackets or braces, and any function whose arguments are parenthesized. Actually, there aren’t really any functions in this sense, just list operators and unary operators behaving as functions because you put parentheses around their arguments. Nevertheless, the name of Chapter 27 is Functions.
Now listen carefully. Here are a couple of rules that are very
important and simplify things greatly, but may occasionally produce
counterintuitive results for the unwary. If any list operator (such as
print) or any named unary operator
(such as chdir) is followed by a left
parenthesis as the next token (ignoring whitespace), the operator and its
parenthesized arguments are given highest precedence, as if it were a
normal function call. The rule is this: if it looks
like a function call, it is a function call. You can
make it look like a nonfunction by prefixing the parentheses with a unary
plus, which does absolutely nothing, semantically speaking—it doesn’t even
coerce the argument to be numeric.
For example, since || has lower precedence than chdir, we get:
chdir $foo || die; # (chdir $foo) || die chdir($foo) || die; # (chdir $foo) || die chdir ($foo) || die; # (chdir $foo) || die chdir +($foo) || die; # (chdir $foo) || die
but because * has higher
precedence than chdir, we get:
chdir $foo * 20; # chdir ($foo * 20) chdir($foo) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access