Operator Characteristics

Learn about operator characteristics in Perl.

Some people call Perl an operator-oriented language. A Perl operator is a series of one or more symbols used as part of the syntax of a language. Each operator operates on zero or more operands. Think of an operator as a special function the parser understands and its operands as arguments.

We’ve seen how Perl manages context through its operators. To understand Perl fully, we must understand how operators interact with their operands.

Operator characteristics

Every operator possesses several important characteristics that govern its behavior: the number of operands on which it operates, its relationship to other operators, the contexts it enforces, and the syntax it provides.

perldoc perlop and perldoc perlsyn provide voluminous information about Perl’s operators, but the docs assume we’re already familiar with a few essential computer science concepts. Fortunately, we’ll recognize these ideas from written language and elementary mathematics, even if we’ve never heard their complicated names before.

Precedence

The precedence of an operator governs when Perl should evaluate it in an expression. Perl evaluates the operator with the highest precedence first, then the next highest, all the way to the lowest precedence. Remember basic math? Multiply and divide before we add and subtract. That’s precedence. Because the precedence of multiplication is higher than the precedence of addition, in Perl 7 + 7 * 10 evaluates to 77, not 140.

We use grouping parentheses to force the evaluation of some operators before others. In (7 + 7) * 10, grouping the addition into a single unit forces its evaluation before the multiplication—though Perl wants to perform the multiplication first, it has to evaluate the grouped subexpression into a single value as the multiplication operator’s left operand. The result is 140.

perldoc perlop contains a table of precedence. Skim it a few times, but don’t bother memorizing it (almost no one does). Spend the time simplifying the code where you can. Then, add parentheses where they clarify.

In cases where two operators have the same precedence, other factors such as associativity and 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 and then adds 4 to the result, not that order of evaluation matters. Exponentiation is right-associative, such that 2 ** 3 ** 4 evaluates 3 ** 4 first and then raises 2 to the 81st power. Use parentheses while writing code like this.

We’ll be fine if we memorize only the precedence and associativity of the common mathematical operators. Simplify the code, and we won’t have to memorize other associativities. If we can’t simplify our code (or if we’re maintaining code and trying to understand it), we use the core B::Deparse module to see precisely how Perl handles operator precedence and associativity.

Run perl -MO=Deparse,-p on a snippet of code. The -p flag adds extra grouping parentheses, often clarifying evaluation order. Beware that Perl’s optimizer will simplify mathematical operations using constant values. If we need to deparse a complex expression, use named variables instead of constant values, as in $x ** $y ** $z.

Get hands-on with 1200+ tech skills courses.