Perl Tutorial
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Perl - Operators

Operators are used to perform operation on a single operand or two operands. Operators in Perl can be categorized as follows:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators
  • Miscellaneous operators

Perl Arithmetic operators

Arithmetic operators are used to perform arithmetic operations on two operands.

+AdditionAdd two values
-SubtractionSubtract one value from another
*MultiplicationMultiply two values
/DivisionDivide one value by another
%ModuloReturns remainder of division operation
**Exponent / PowerReturns first operand raised to the power of second operand


Perl Assignment operators

Assignment operators are used to assign values of right hand side expression to left hand side operand.

OperatorExpressionEquivalent toDescription
=a = 5a = 5Example
+=a += ba = a + b
-=a -= ba = a - b
*=a *= ba = a * b
/=a /= ba = a / b
%=a %= ba = a % b
**=a **= ba = a ** b
&=a &= ba = a & bMore Info
|=a |= b a = a | bMore Info
^=a ^= b a = a ^ bMore Info
>>=a >>= ba = a >> bMore Info
<<=a <<= ba = a << bMore Info

Perl Comparison operators

Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.

!= Not equal
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to


Perl Logical operators

Logical operators are used to combine two or more conditions.

Operator NameDescription
and Logical ANDReturns true when all conditions are true
orLogical ORReturns true when any of the conditions is true
notLogical NOTReturns true when given conditions is not true

More Info

Perl Bitwise operators

Bitwise operators are used to perform bitwise operations on two operands.

OperatorNameDescriptionMore Info
&ANDReturns 1 if both bits at the same position in both operands are 1, else returns 0.More Info
|ORReturns 1 if one of two bits at the same position in both operands is 1, else returns 0.More Info
^XORReturns 1 if only one of two bits at the same position in both operands is 1, else returns 0.More Info
~NOTReverse all the bits.More Info
>>Right shiftThe left operand is moved right by the number of bits present in the right operand.More Info
<<Left shiftThe left operand value is moved left by the number of bits present in the right operand.More Info

Perl Miscellaneous operators

The below table describes other operators supported by Perl:

ternary operator (?:)Returns one of the two values based on value of boolean expression.

Perl Operators Precedence

Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.

For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.

The following table lists the precedence and associativity of Perl operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence. When operators have the same precedence, associativity of the operators determines the order in which the operations are performed.

25terms and list operatorsterms and list operators (leftward)Left to Right
24->Arrow operator
23++a  --aPrefix increment, Prefix decrementnon-associative
a++  a--Postfix increment, Postfix decrement
22**Exponentiation operatorRight to Left
21!Logical NOT
~Bitwise NOT
~.Bitwise string NOT
\Reference operator
+a  -aUnary plus and minus
20=~  !~Pattern matching operatorsLeft to Right
19*  /  %Multiplication, Division, Remainder
xRepetition operator
18+  -  .Addition, Subtraction, Concatenating operator
17<<  >>Bitwise left shift and right shift
16Named unary operatorsNamed unary operatorsnon-associative
15isaClass instance Operator
14<  <=  >  >=Less than, Less than or equal, Greater than, and Greater than or equalchained
lt  le  gt  geString comparison operators: Less than, Less than or equal, Greater than, and Greater than or equalchained
13==  !=Equality and inequality operatorchain/na
eq  neString equality and inequality operator
<=>Two sided comparison operator
cmpString two sided comparison operator
~~Smartmatch operator
12&  &.Bitwise AND, Bitwise string ANDLeft to Right
11|  |.Bitwise OR, Bitwise string OR
^  ^.Bitwise XOR, Bitwise string XOR
10&&C-style Logical AND operator
9||C-style Logical OR operator
//Perl-style Logical OR operator
8..Range operatornon-associative
...yada-yada operator
7a?b:cternary (conditional) operatorRight to Left
6=Direct assignment
+=  -=  *=  /=  %=  **=Compound assignment by addition, subtraction, multiplication, division, remainder, and exponentiation
<<=  >>=Compound assignment by Bitwise left shift and right shift
&=  ^=  |=Compound assignment by Bitwise AND, XOR and OR
&.=  ^.=  |.=Compound assignment by Bitwise string AND, XOR and OR
gotogoto statement
lastlast statement
nextnext statement
redoredo statement
dumpdump statement
5,  =>Comma operatorsLeft to Right
4list operatorslist operators (rightward)non assoc
3notLogical NOTRight to Left
2andLogical ANDLeft to Right
1orLogical OR
xorBitwise XOR