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
- Increment/Decrement operators
- Logical operators
- Bitwise operators
- Quote-Like operators
- Miscellaneous operators
Perl Arithmetic operators
Arithmetic operators are used to perform arithmetic operations on two operands.
Operator | Name | Description |
---|---|---|
+ | Addition | Add two values |
- | Subtraction | Subtract one value from another |
* | Multiplication | Multiply two values |
/ | Division | Divide one value by another |
% | Modulo | Returns remainder of division operation |
** | Exponent / Power | Returns first operand raised to the power of second operand |
Example
Perl Assignment operators
Assignment operators are used to assign values of right hand side expression to left hand side operand.
Operator | Expression | Equivalent to | Description |
---|---|---|---|
= | a = 5 | a = 5 | Example |
+= | a += b | a = a + b | |
-= | a -= b | a = a - b | |
*= | a *= b | a = a * b | |
/= | a /= b | a = a / b | |
%= | a %= b | a = a % b | |
**= | a **= b | a = a ** b | |
&= | a &= b | a = a & b | More Info |
|= | a |= b | a = a | b | More Info |
^= | a ^= b | a = a ^ b | More Info |
>>= | a >>= b | a = a >> b | More Info |
<<= | a <<= b | a = a << b | More 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. Perl has two sets of comparison operators. One is used for comparing numerical values while other is used for comparing string values.
Numeric Comparison operators
Operator | Description |
---|---|
== | Equal: Checks the values of two operands and returns true if they are same. |
!= | Not equal: Checks the values of two operands and returns true if they are not same. |
> | Greater than: Checks the values of two operands and returns true if the value of first operand is greater than the value of second operand. |
< | Less than: Checks the values of two operands and returns true if the value of first operand is less than the value of second operand. |
>= | Greater than or equal to: Checks the values of two operands and returns true if the value of first operand is greater than or equal to the value of second operand. |
<= | Less than or equal to: Checks the values of two operands and returns true if the value of first operand is less than or equal to the value of second operand. |
<=> | Comparison Operator: Checks the values of two operands and returns values based on the values of two operands:
|
Example
String Comparison operators
Operator | Description |
---|---|
eq | Equal: Checks the values of two operands and returns true if they are string-wise same. |
ne | Not equal: Checks the values of two operands and returns true if they are string-wise not same. |
gt | Greater than: Checks the values of two operands and returns true if the first operand is string-wise greater than the second operand. |
lt | Less than: Checks the values of two operands and returns true if the first operand is string-wise less than the second operand. |
ge | Greater than or equal to: Checks the values of two operands and returns true if the first operand is string-wise greater than or equal to the second operand. |
le | Less than or equal to: Checks the values of two operands and returns true if the first operand is string-wise less than or equal to the second operand. |
cmp | Comparison Operator: Checks the values of two operands and returns values based on the values of two operands:
|
Example
Perl Increment/Decrement operators
Increment and decrement operators are used to increase and decrease the value of variable.
Operator | Name | Description | Example |
---|---|---|---|
++$x | Pre-increment | Increases the value of $x by 1, then returns $x. | Example |
$x++ | Post-increment | Returns $x, then increases the value of $x by 1. | |
--$x | Pre-decrement | Decreases the value of $x by 1, then returns $x. | Example |
$x-- | Post-decrement | Returns $x, then decreases the value of $x by 1. |
Perl Logical operators
Logical operators are used to combine two or more conditions.
Operator | Name | Description |
---|---|---|
and | Logical AND | Returns true when all conditions are true |
&& | ||
or | Logical OR | Returns true when any of the conditions is true |
|| | ||
not | Logical NOT | Returns true when given conditions is not true |
! |
More Info
Perl Bitwise operators
Bitwise operators are used to perform bitwise operations on two operands.
Operator | Name | Description | More Info |
---|---|---|---|
& | AND | Returns 1 if both bits at the same position in both operands are 1, else returns 0. | More Info |
| | OR | Returns 1 if one of two bits at the same position in both operands is 1, else returns 0. | More Info |
^ | XOR | Returns 1 if only one of two bits at the same position in both operands is 1, else returns 0. | More Info |
~ | NOT | Reverse all the bits. | More Info |
>> | Right shift | The left operand is moved right by the number of bits present in the right operand. | More Info |
<< | Left shift | The left operand value is moved left by the number of bits present in the right operand. | More Info |
Quote-Like operators
Perl supports the following Quote-like operators:
Operator | Description |
---|---|
q{ } | Encloses a string with-in single quotes. |
qq{ } | Encloses a interpolated string with-in double quotes. |
qx{ } | Encloses a (possibly) interpolated string and then executed as a system command, via /bin/sh or its equivalent if required. |
Perl Miscellaneous operators
The table below describes other operators supported by Perl:
Operator | Description |
---|---|
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.
Precedence | Operator | Description | Associativity |
---|---|---|---|
25 | terms and list operators | terms and list operators (leftward) | Left to Right |
24 | -> | Arrow operator | |
23 | ++a --a | Prefix increment, Prefix decrement | non-associative |
a++ a-- | Postfix increment, Postfix decrement | ||
22 | ** | Exponentiation operator | Right to Left |
21 | ! | Logical NOT | |
~ | Bitwise NOT | ||
~. | Bitwise string NOT | ||
\ | Reference operator | ||
+a -a | Unary plus and minus | ||
20 | =~ !~ | Pattern matching operators | Left to Right |
19 | * / % | Multiplication, Division, Remainder | |
x | Repetition operator | ||
18 | + - . | Addition, Subtraction, Concatenating operator | |
17 | << >> | Bitwise left shift and right shift | |
16 | Named unary operators | Named unary operators | non-associative |
15 | isa | Class instance Operator | |
14 | < <= > >= | Less than, Less than or equal, Greater than, and Greater than or equal | chained |
lt le gt ge | String comparison operators: Less than, Less than or equal, Greater than, and Greater than or equal | chained | |
13 | == != | Equality and inequality operator | chain/na |
eq ne | String equality and inequality operator | ||
<=> | Two sided comparison operator | ||
cmp | String two sided comparison operator | ||
~~ | Smartmatch operator | ||
12 | & &. | Bitwise AND, Bitwise string AND | Left 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 operator | non-associative |
... | yada-yada operator | ||
7 | a?b:c | ternary (conditional) operator | Right 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 | ||
goto | goto statement | ||
last | last statement | ||
next | next statement | ||
redo | redo statement | ||
dump | dump statement | ||
5 | , => | Comma operators | Left to Right |
4 | list operators | list operators (rightward) | non assoc |
3 | not | Logical NOT | Right to Left |
2 | and | Logical AND | Left to Right |
1 | or | Logical OR | |
xor | Bitwise XOR |