Perl - Operators Precedence
Perl Operators Precedence
Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.
For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.
Operator Associativity
Operator associativity is the direction in which an expression is evaluated. For example:
$x = 10; $y = 20; #associativity of = operator is #Left to Right, hence x will become 20 $x = $y;
As the associativity of = is left to right. Hence x is assigned the value of y.
Example:
The example below illustrates the operator precedence in Perl.
#evaluates 5 * 2 first $retval1 = 15 - 5 * 2; #above expression is equivalent to $retval2 = 15 - (5 * 2); #forcing compiler to evaluate 15 - 5 first $retval3 = (15 - 5) * 2; print("15 - 5 * 2 = $retval1\n"); print("15 - (5 * 2) = $retval2\n"); print("(15 - 5) * 2 = $retval3\n");
The output of the above code will be:
15 - 5 * 2 = 5 15 - (5 * 2) = 5 (15 - 5) * 2 = 20
Perl Operators Precedence Table
The following table lists the precedence and associativity of Perl operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.
Precedence | Operator | Description | Associativity |
---|---|---|---|
25 | terms and list operators | terms and list operators (leftward) | Left to Right |
24 | -> | Arrow operator | |
23 | ++a --a | Prefix increment, Prefix decrement | non-associative |
a++ a-- | Postfix increment, Postfix decrement | ||
22 | ** | Exponentiation operator | Right to Left |
21 | ! | Logical NOT | |
~ | Bitwise NOT | ||
~. | Bitwise string NOT | ||
\ | Reference operator | ||
+a -a | Unary plus and minus | ||
20 | =~ !~ | Pattern matching operators | Left to Right |
19 | * / % | Multiplication, Division, Remainder | |
x | Repetition operator | ||
18 | + - . | Addition, Subtraction, Concatenating operator | |
17 | << >> | Bitwise left shift and right shift | |
16 | Named unary operators | Named unary operators | non-associative |
15 | isa | Class instance Operator | |
14 | < <= > >= | Less than, Less than or equal, Greater than, and Greater than or equal | chained |
lt le gt ge | String comparison operators: Less than, Less than or equal, Greater than, and Greater than or equal | chained | |
13 | == != | Equality and inequality operator | chain/na |
eq ne | String equality and inequality operator | ||
<=> | Two sided comparison operator | ||
cmp | String two sided comparison operator | ||
~~ | Smartmatch operator | ||
12 | & &. | Bitwise AND, Bitwise string AND | Left to Right |
11 | | |. | Bitwise OR, Bitwise string OR | |
^ ^. | Bitwise XOR, Bitwise string XOR | ||
10 | && | C-style Logical AND operator | |
9 | || | C-style Logical OR operator | |
// | Perl-style Logical OR operator | ||
8 | .. | Range operator | non-associative |
... | yada-yada operator | ||
7 | a?b:c | ternary (conditional) operator | Right to Left |
6 | = | Direct assignment | |
+= -= *= /= %= **= | Compound assignment by addition, subtraction, multiplication, division, remainder, and exponentiation | ||
<<= >>= | Compound assignment by Bitwise left shift and right shift | ||
&= ^= |= | Compound assignment by Bitwise AND, XOR and OR | ||
&.= ^.= |.= | Compound assignment by Bitwise string AND, XOR and OR | ||
goto | goto statement | ||
last | last statement | ||
next | next statement | ||
redo | redo statement | ||
dump | dump statement | ||
5 | , => | Comma operators | Left to Right |
4 | list operators | list operators (rightward) | non assoc |
3 | not | Logical NOT | Right to Left |
2 | and | Logical AND | Left to Right |
1 | or | Logical OR | |
xor | Bitwise XOR |
❮ Perl - Operators