|Perl Language Reference Manual|
by Larry Wall and others
Paperback (6"x9"), 724 pages
RRP £29.95 ($39.95)
Sales of this book support The Perl Foundation! Get a printed copy>>>
7.1 Operator Precedence and Associativity
Operator precedence and associativity work in Perl more or less like they do in mathematics.
Operator precedence means some operators are evaluated before
others. For example, in
2 + 4 * 5, the multiplication has higher
4 * 5 is evaluated first yielding
2 + 20 ==
22 and not
6 * 5 == 30.
Operator associativity defines what happens if a sequence of the
same operators is used one after another: whether the evaluator will
evaluate the left operations first or the right. For example, in
- 4 - 2, subtraction is left associative so Perl evaluates the
expression left to right.
8 - 4 is evaluated first making the
4 - 2 == 2 and not
8 - 2 == 6.
Perl operators have the following associativity and precedence, listed from highest precedence to lowest. Operators borrowed from C keep the same precedence relationship with each other, even where C's precedence is slightly screwy. (This makes learning Perl easier for C folks.) With very few exceptions, these all operate on scalar values only, not array values.
left terms and list operators (leftward) left
! ~ \ and unary + and -left
* / % xleft
+ - .left
<< >>nonassoc named unary operators nonassoc
< > <= >= lt gt le genonassoc
== != <=> eq ne cmp ~~left
= += -= *= etc.left
, =>nonassoc list operators (rightward) right
In the following sections, these operators are covered in precedence order.
Many operators can be overloaded for objects. See "Package for overloading Perl operations" (overload) in the Perl Library Reference Manual (Volume 1).
|ISBN 9781906966027||Perl Language Reference Manual||See the print edition|