Skip to content

Latest commit

 

History

History
186 lines (145 loc) · 6.06 KB

operator_characteristics.pod

File metadata and controls

186 lines (145 loc) · 6.06 KB

Operator Characteristics

Every operator possesses several important characteristics which govern its behavior: the number of operands on which it operates, its relationship to other operators, and its syntactic possibilities.

perldoc perlop and perldoc perlsyn provide voluminous information about Perl's operators, but the documentation assumes you're already familiar with some details of how they work. The essential computer science concepts may sound imposing at first, but once you get past their names, they're straightforward. You already understand them implicitly.

Precedence

The precedence of an operator governs when Perl should evaluate it in an expression. Evaluation order proceeds from highest to lowest precedence. Because the precedence of multiplication is higher than the precedence of addition, 7 + 7 * 10 evaluates to 77, not 140.

To force the evaluation of some operators before others, group their subexpressions in parentheses. In (7 + 7) * 10, grouping the addition into a single unit forces its evaluation before the multiplication. The result is 140.

perldoc perlop contains a table of precedence. Read it, understand it, but don't bother memorizing it (almost no one does). Spend your time keeping your expressions simple, and then add parentheses to clarify your intent.

In cases where two operators have the same precedence, other factors such as associativity (associativity) and fixity (fixity) break the tie.

Associativity

The associativity of an operator governs whether it evaluates from left to right or right to left. Addition is left associative, such that 2 + 3 + 4 evaluates 2 + 3 first, then adds 4 to the result. Exponentiation is right associative, such that 2 ** 3 ** 4 evaluates 3 ** 4 first, then raises 2 to the 81st power.

It's worth your time to memorize the precedence and associativity of the common mathematical operators, but again simplicity rules the day. Use parentheses to make your intentions clear.

Arity

The arity of an operator is the number of operands on which it operates. A nullary operator operates on zero operands. A unary operator operates on one operand. A binary operator operates on two operands. A trinary operator operates on three operands. A listary operator operates on a list of operands. An operator's documentation and examples should make its arity clear.

For example, the arithmetic operators are binary operators, and are usually left associative. 2 + 3 - 4 evaluates 2 + 3 first; addition and subtraction have the same precedence, but they're left associative and binary, so the proper evaluation order applies the leftmost operator (+) to the leftmost two operands (2 and 3) with the leftmost operator (+), then applies the rightmost operator (-) to the result of the first operation and the rightmost operand (4).

Perl novices often find confusion between the interaction of listary operators--especially function calls--and nested expressions. Where parentheses usually help, beware of the parsing complexity of:

... which prints the value 6 and (probably) evaluates as a whole to 4 (the return value of say multiplied by 4). Perl's parser happily interprets the parentheses as postcircumfix (fixity) operators denoting the arguments to say, not circumfix parentheses grouping an expression to change precedence.

Fixity

An operator's fixity is its position relative to its operands:

  • Infix operators appear between their operands. Most mathematical operators are infix operators, such as the multiplication operator in $length * $width.

  • Prefix operators precede their operators. Postfix operators follow. These operators tend to be unary, such as mathematic negation (-$x), boolean negation (!$y), and postfix increment ($z++).

  • Circumfix operators surround their operands, as with the anonymous hash constructor ({ ... }) and quoting operators (qq[ ... ]).

  • Postcircumfix operators follow certain operands and surround others, as seen in hash and array element access ($hash{$x} and $array[$y]).

POD ERRORS

Hey! The above document had some coding errors, which are explained below:

Around line 3:

A non-empty Z<>

Around line 18:

A non-empty Z<>

Around line 40:

A non-empty Z<>

Around line 76:

A non-empty Z<>

Around line 120:

A non-empty Z<>