A perl operator is a series of symbols used as syntax. An operator is a sort of function and its operands are arguments.
Perl precedence acts like BODMAS in Mathematics. Addition and Subtraction always comes after the Multiplication and Division.
8 + 4 - 5 * 6 / 3 = 2
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.
use 5.010; use strict; use warnings; my $result1 = 8 + 4 - 5 * 6 / 3 ; say $result1; my $result2 = 12 * 3 + 2 ** 2 << 1; say $result2;
|++, --||Auto-increment, Auto-decrement|
|-, ~, !||Operators having one operand|
|=~, !~||Pattern matching operators|
|*, /, %, x||Multiplication, Divisor, Remainder, Repetition|
|+, -, .||Addition, Subtraction, Concatenation|
|<<, >>||Shifting operators|
|-e, -r||File status operators|
|<, <=, >, >=, lt, le, gt, ge||Inequality comparison operators|
|==, !=, <=>, eq, nq, cmp||Equality comparison operators|
||, ^||Bitwise OR and XOR|
|. .||List range operators|
|? and :||Conditional operators|
|=, +=, -=, *=||Assignment operators|
|not||low precedence logical NOT|
|and||low precedence logical AND|
|or, xor||low precedence logical OR and XOR|
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.
7 + 4 + 2 = 13
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.
use 5.010; use strict; use warnings; my $result = 3 ** 2 ** 3; say $result;
|++, --||Order of direction is not applicable here|
|-, ~, !||Right-to-Left|
|*, /, %, x||Left-to-Right|
|+, -, .||Left-to-Right|
|-e, -r||Order of direction is not applicable here|
|<, <=, >, >=, lt, le, gt, ge||Left-to-Right|
|==, !=, <=>, eq, ne, cmp||Left-to-Right|
|? and :||Right-to-Left|
|=, +=, -=, *=||Right-to-Left|
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.
3 + 3 ? 2
Arithmetic operators are usually left associative. Here, (3 + 3) evaluates first and then goes to the second (-) operator.
use 5.010; use strict; use warnings; my $result = ( 5 - 2 + 10 ) * 2; say $result;
Operator fixity can be defined as its position relative to its operands.
3 + 2
Here, + operator appears in between the operands 3 and 2
! $a, - 3x
Here, ! and - operator appears before the operands $a and 3.
Here, ++ operator appears after the operands $x.