Close
Close

Operators in Perl


Operators are symbols which tell the compiler to perform certain operations on variables. You can take, for example, (*) which is an operator used for multiplying the values of two variables.

There are different types of operators in Perl. Let's take a look at some of the most important operators with a few examples for each.

Arithmetic Operations


Arithmetic Operators are the type of operators which take numerical values (either literals or variables) as their operands and return a single numerical value.

Let's assume 'a' takes value 5 and 'b' takes value 2.

Operator Description Example
+ Adds operands a+b=7
- Subtracts second operand from the first a-b=3
* Multiplies both operands a*b=10
/ Divides numerator by denominator. a/b=2.5
% Modulus Operator returns the remainder of after an integer division. a%b=1
** Exponent Operator performs exponential (power) calculation on operators. a**b=25

$a = 5;
$b = 2;
print $a+$b,"\n";
print $a-$b,"\n";
print $a*$b,"\n";
print $a/$b,"\n";
print $a%$b,"\n";
print $a**$b,"\n";
Output
7
3
10
2.5
1
25

Operator Precedence and Associativity


In Maths, you might have learned about BODMAS rule, but that rule is not applied here. If we have written more than one operation in one line, then which operation should be done first is governed by the following rules :-

Expressions inside brackets '()' are evaluated first. After that, this table is followed:

Note that there are a number of operators in this table you haven't learned about yet.

Operators at the top have higher precedence than the lower ones.

Just have a look at the table. You will get these things from the examples given after this table.

Operator Associativity
-> left
++ -- none
** right
! ~ \ + - right
=~ !~ left
* / % x left
+ - . left
<< >> left
unary operators none
< > <= >= lt gt le ge none
== != <=> eq ne cmp none
& left
| ^ left
&& left
|| left
.. none
?: right
= += -= *= /=, etc. right
, => left
list operators none
not right
and left
or xor left

Now, suppose you have an expression $k = 8/4+3-5*6+8 to solve
Highest priority operators are '*' and '/', as they are on the top of the table.
Since both operators have the same precedence, so we will follow associativity and it is left. So, we will solve from left to right.
Solving, '*' and '/' first from left to right.
$k = 2+3-30+8
Now solving, '+' and '-' from left to right (Its associativity is right).
$k = -17
Now solving '=' from right to left
k is -17.

$k = 8/4+3-5*6+8;
print $k,"\n";
Output
-17
If you don't want to remember these rules, then just put the expression you want to execute first in brackets. Eg- If you want to divide the product of (2+2) and 444 by quotient of (999/5) then write this expression - ((2+2)*444)/(999/5). This will get simplified to (4*444)/(999/5) and then further to 1776/199.8.

Let's see one more example.

$a = 1;
$a = $a+1;
print $a,"\n";
$a = $a*5;
print $a,"\n";
Output
2
10

It is very simple. '+' operator has higher precedence than '=' operator. So, '$a+1' will be evaluated first and it will be 2. Then '=' operator will be evaluated giving '$a = 2;'. So, now 'a' is 2.
Similarly, $a*5 will be first evaluated as '5*2', then '=' operator will be evaluated ($a = 10;) and 'a' will become 10.

Assignment Operators


Assignment Operators are used to assign values of the operand on the right to the operand on the left. The most common assignment operator is =.

If we write $a = 10; means that we are assigning a value '10' to the variable 'a'.

There are more assignment operators which are listed below.

Operator Description Example
= Assigns value of the right operand to the left operand $C = $A + $B
+= Adds the value of right operand to the left operand and assigns the final value to the left operand $C += $A is same as $C = $C + $A
-= Subtracts the value of right operand to the left operand and assigns the final value to the left operand $C -= $A is same as $C = $C - $A
*= Multiplies the value of right operand to the left operand and assigns the final value to the left operand $C *= $A is same as $C = $C * $A
/= Divides the value of left operand from the right operand and assigns the final value to the left operand $C /= $A is same as $C = $C / $A
%= takes modulus using two operands and assigns the result to the left operand $C %= $A is same as $C = $C % $A
**= performs exponential operation and assigns the result to the left operand $C **= $A is same as $C = $C**$A

Let's take an example:

$a = 25;
$b = 8;
$a += $b;
print $a,"\n";
$a -= $b;
print $a,"\n";
$a *= $b;
print $a,"\n";
$a /= $b;
print $a,"\n";
$a %= $b;
print $a,"\n";
$a **= $b;
print $a,"\n";
Output
33
25
200
25
1
1

Initially 'a' is 25 and 'b' is 8.
$a += $b; - It means that $a = $a+$b. Now, a is a+b i.e. 33. ( $a = $a+$b )
$a -= $b - Now, a is 33. So, $a -= $b ( $a = $a-$b ) is 25. Thus, the new 'a' is 25. The same is for the next cases.

Relational Operators


Relational Operators check the relationship between two operands.

Following is the list of relational operators in Perl for numeric values.

Assume the value of 'a' to be 8 and that of 'b' to be 4.

Operator Description Example
== Equal to ($a == $b) is false
!= Not equal to ($a != $b) is true
<=> Compares two operands, returns -1, 0, or 1 depending on whether the left argument is numerically less than, equal to, or greater than the right argument. ($a <=> $b) will return 1
> Greater than ($a > $b) is true
< Less than ($a < $b) is false
>= Greater than or equal to ($a >= $b) is true
<= Less than or equal to ($a <= $b) is false

Following is the list of relational operators in Perl for string values.

We use six comparison operators to compare strings, to check if one string is alphabetically bigger, smaller or equal to another. Here are the string operators and numerical operators they correspond to:

String Operator Numerical Operator
eq ==
ne !=
gt >
lt <
ge >=
le <=

Difference between = and ==


Although = and == seem to be same, but they are quite different from each other. = is the assignment operator while == is the equality operator. = is used to assign values whereas == is used for comparing values.

Take two examples.

$x = 5;

$x == 5;

By writing $x = 5, we assign a value of 5 to 'x', whereas by writing $x == 5, we check if the value of x is 5 or not.

diferrence in = and ==

More operators


Assume that the value of 'a' is 4.

Operator Description Example
. concatenates two strings "ab"."cd" is "abcd"
.. range operator (2..4) will give (2, 3, 4)
++ Increment operator $a++ will give 5
-- Decrement operator $a-- will give 3
x Repetition operator x "a"x3 will give "aaa"
-> Used for dereferencing a method or a variable from an object or a class name You will study later

You will not learn all the operators mentioned above in this chapter. Let's see an example:

$a = 25;
print $a++,"\n";
print $a--,"\n";
print "a"x3,"\n";
Output
25
26
aaa

Ask Yours
Post Yours
Doubt? Ask question