Perl Operators

A perl operator is a series of symbols used as syntax. An operator is a sort of function and its operands are arguments.


Perl Operator Precedence

Perl precedence acts like BODMAS in Mathematics. Addition and Subtraction always comes after the Multiplication and Division.

For example,

Here, answer will be 2 with BODMAS rule. (6 / 3 = 2) will be calculated first, then the quotient 2 will be multiplied by the 5, followed by subtraction and addition.

Example:

Output:

2
80

Perl Operator Precedence Table

OperatorsDescription
++, --Auto-increment, Auto-decrement
-, ~, !Operators having one operand
**Exponentiation
=~, !~Pattern matching operators
*, /, %, xMultiplication, Divisor, Remainder, Repetition
+, -, .Addition, Subtraction, Concatenation
<<, >>Shifting operators
-e, -rFile status operators
<, <=, >, >=, lt, le, gt, geInequality comparison operators
==, !=, <=>, eq, nq, cmpEquality comparison operators
&Bitwise AND
|, ^Bitwise OR and XOR
&&Logical AND
||Logical OR
. .List range operators
? and :Conditional operators
=, +=, -=, *=Assignment operators
,Comma operator
notlow precedence logical NOT
andlow precedence logical AND
or, xorlow precedence logical OR and XOR

Perl Operator Associativity

The associativity of an operator helps you to decide whether to evaluate an equation from (left to right) or (right to left).

The order of operation is very important. Sometimes it is same from both the sides but sometimes it produces drastic difference.

For example,

The answer for this question is same in any order either from left or right.

3 ∗∗ 2 ∗∗ 3

The answer for this question will be (9 ∗∗ 3) from left and (3 ∗∗ 8) from right. Both the answers have a lot of difference.

Example:

Output:

6561

Perl Associativity Table

OperatorsDescription
++, --Order of direction is not applicable here
-, ~, !Right-to-Left
**Right-to-Left
=~, !~Left-to-Right
*, /, %, xLeft-to-Right
+, -, .Left-to-Right
<<, >>Left-to-Right
-e, -rOrder of direction is not applicable here
<, <=, >, >=, lt, le, gt, geLeft-to-Right
==, !=, <=>, eq, ne, cmpLeft-to-Right
&Left-to-Right
|, ^Left-to-Right
&&Left-to-Right
||Left-to-Right
..Left-to-Right
? and :Right-to-Left
=, +=, -=, *=Right-to-Left
,Left-to-Right
notLeft-to-Right
andLeft-to-Right
or, xorLeft-to-Right

Perl Arity

The arity of an operator can be defined as the number of operands on which it operates.

A nullary operator operates on zero operand, a unary operator operates on one operand, a binary operator operates on two operands and a listary operator operates on list of operands.

For example,

Arithmetic operators are usually left associative. Here, (3 + 3) evaluates first and then goes to the second (-) operator.

Example:

Output:

26

Perl Fixity

Operator fixity can be defined as its position relative to its operands.

For example,

  • Infix operator appears between its operands..

    3 + 2

    Here, + operator appears in between the operands 3 and 2

  • Prefix operator appears before its operands.

    ! $a, - 3x

    Here, ! and - operator appears before the operands $a and 3.

  • Postfix operator appears after its operands,.

    $x ++

    Here, ++ operator appears after the operands $x.

  • Circumfix operators surround its operands. Such as hash constructor and quoting operators..

    (qq[...])

  • Postcircumfix operators follow certain operands and surround some operands.

    $hash{$a}