DavidSzczesniak
12/14/2017 - 11:22 AM

Precedence, Associativity, Arity and Fixity

Each operator posseses several important characteristics that govern its behaviour. Here, they are briefly explained.

\!h Precedence
Governs when Perl should evaluate an operator in an expression. 
Evaluating the one with the highest precedence first, then the next highest, all the way to the lowest.

For example, multiplication has a higher precedence than addition so: 7 + 7 * 10 = 77, not 140.
Use parentheses to force evaluation of some operators before others. Now (7 + 7) *10 does = 140
For reference, 'perldoc perlop' contains a table of precedence.

\!h Associativity 
In cases where two operators have the same precedence, other factors break the tie; this is one of them.
Associativity governs whether an operators evaluates from left to right or right to left. 

For example, addition is left associative. Therefore, 2 + 3 + 4 evaluates 2 + 3 first,then adds 4 to the result.
Its best to memorize at least the precedence and associativity of the common mathematical operators.

\!h Arity 
The arity of an operator is the number of operands on which it operates. 
Nullary operator = operates on 0 operands
Unary operator   = 1 operand
Binary operator  = 2 
Trinary operator = 3 
Listary operator = on a list of zero or more

For example, 2 + 3 - 4 evaluates 2 + 3 first, even though addition and subtraction have the same precedence.
However, they are left-associative and binary.

\!h Fixity 
This is an operator's position relative to its operands:
Infix operators         - appear between their operands.
Prefix operators        - come before their operands (precede).
Circumfix operators     - surround their operands, like parentheses.
Postcircumfix operators - follow certain operators and surround others.