# Associativity and Precedence of Perl Operators

## Perl Operators – Part 10

### Perl Course

Foreword: In this part of the series I talk about associativity and precedence of Perl operators; and I give you the table.

By: Chrysanthus Date Published: 12 Oct 2015

### Introduction

This is part 10 of my series, Perl Operators. In this part of the series I talk about associativity and precedence of Perl operators; and I give you the table. You should have read the previous parts of the series before coming here, as this is a continuation.

Perl operators exists in the following categories: Terms and List Operators (Leftward), The Arrow Operator, Auto-increment and Auto-decrement, Exponentiation, Symbol Unary Operators, Binding Operators, Multiplicative Operators, Additive Operators, Shift Operators, Named Unary Operators, Relational Operators, Equality Operators, Smartmatch Operator, Bitwise And, Bitwise Or and Exclusive Or, C-style Logical And, C-style Logical Or, Logical Defined-Or, Range Operators, Conditional Operator, Assignment Operators, Comma Operator, List Operators (Rightward), Logical Not, Logical And, Logical or and Exclusive Or, Quote-Like Operators, I/O Operators, Bitwise String Operators.

Associativity
If you have a sequence of additive operators, the one on the left will be evaluated first, followed by the one next to it coming to the right, followed by the one next to it, rightward, and so on. Consider the following code:

my \$ret = 9 - 5 - 2;

print \$ret;

The output is:

2

The output is 2 because 9-5 is evaluated first and then the result of 4 is used to evaluate 4-2. If 5-2 had been evaluated first, before 9-3, the output would have been 6. That is left associativity. Right associativity is in the opposite direction.

Precedence
Operators in Perl exist in categories. Different categories are said to have different precedence. Operators in the same category have the same precedence and same associativity. Associativity is left or right. Precedence is a question of level.

Now, when a sequence of operators in the same category are used one after another, the one on the left or right is evaluated first, depending on the said associativity (left or right) for the category. However, when the sequence of operators are of different categories, the one with the said highest precedence is evaluated first; it does not matter which one is on the left or on the right. For example, in

2 + 4 * 5

, multiplication and addition are in different categories. The multiplication (category) has higher precedence than addition; so 4 * 5 is evaluated first to give 20. The expression becomes 2 + 20 to give 22. If the order was changed to 4 * 5 + 2, you will still have the same result. That is 4 * 5 of the higher precedence than 5 + 2 will be evaluated first to end up with 20+2, independent of what is on the left or right.

As you can see, when there is a sequence of operators, one has to be evaluated first, before another one, before another, and so on. Which one is evaluated first? If the operators are of the same category, the one that is evaluated first depends on whether the category is left or right associative. If two consecutive operators are of different categories, the one with the higher precedence is evaluated first; it does not matter whether it is on the left or on the right.

List Operators
An example of a list operator is the print operator. Consider the following statements:

print "man", "woman";

and

print ("man", "woman");

The second print expression is a better list operator: the combination, print() with the parentheses. The first expression is not quite good. There, you have the quotation as an operator, the comma as an operator and print itself as an operator, ending up with operators in sequence, with relative arguments. In the second case the arguments with their operators are nested in the list operator, which is print(). A good list operator is like a function call using parentheses.

Term
A TERM has the highest precedence in Perl. They include variables, quote and quote-like operators, any expression in parentheses, and any function whose arguments are parenthesized. Note that do{}, sub{}, and eval{} are also terms. So, print() is better seen as a term than as an operator. A term is a kind of operator.

Table of Associativity and Precedence
The left-most column of the table gives the associativity. The higher the category in the table, the higher the precedence.

### Operator Precedence and Associativity

AssociativityCategory
leftterms and list operators (leftward)
left->
nonassoc ++ --
right **
right ! ~ \ and unary + and -
left =~ !~
left * / % x
left + - .
left << >>
nonassoc named unary operators
nonassoc < > <= >= lt gt le ge
nonassoc == != <=> eq ne cmp ~~
left &
left | ^
left &&
left || //
nonassoc .. ...
right ?:
right = += -= *= etc.
left , =>
nonassoc list operators (rightward)
right not
left and
left or xor

That is it for this part of the series.

Chrys

Perl Basics
Perl Data Types
Perl Syntax
Perl References Optimized
Handling Files and Directories in Perl
Perl Function
Perl Package
Perl Object Oriented Programming
Perl Regular Expressions
Perl Operators
Perl Core Number Basics and Testing
Commonly Used Perl Predefined Functions
Line Oriented Operator and Here-doc
Handling Strings in Perl
Using Perl Arrays
Using Perl Hashes
Perl Multi-Dimensional Array
Date and Time in Perl
Perl Scoping
Namespace in Perl
Perl Eval Function
Writing a Perl Command Line Tool
Perl Insecurities and Prevention
Sending Email with Perl
Miscellaneous Features in Perl
Perl Two-Dimensional Structures