An operator is a character that represents an action, for example + is an arithmetic operator that represents addition.

Operators in perl are categorised as following types:

1) Basic Arithmetic Operators

2) Assignment Operators

3) Auto-increment and Auto-decrement Operators

4) Logical Operators

5) Comparison operators

6) Bitwise Operators

7) Quote and Quote-like Operators

## 1) Basic Arithmetic Operators

Basic arithmetic operators are: +, -, *, /, %, **

**+** is for addition: $x + $y

**–** is for subtraction: $x – $y

***** is for multiplication: $x * $y

**/** is for division: $x / $y

**%** is for modulo: $x % $y

**Note:** It returns remainder, for example 10 % 5 would return 0

****** is for Exponentiation: $x ** $y

x to the power y

#### Example

#!/usr/local/bin/perl $x = -4; $y = 2; $result = $x + $y; print '+ Operator output: ' . $result . "\n"; $result = $x - $y; print '- Operator output: ' . $result . "\n"; $result = $x * $y; print '* Operator output: ' . $result . "\n"; $result = $x / $y; print '/ Operator output: ' . $result . "\n"; $result = $x % $y; print '% Operator output: ' . $result. "\n"; $result = $x ** $y; print '** Operator output: ' . $result . "\n";

**Output:**

+ Operator output: -2 - Operator output: -6 * Operator output: -8 / Operator output: -2 % Operator output: 0 ** Operator output: 16

## 2) Assignment Operators

Assignments operators in perl are: =, +=, -=, *=, /=, %=, **=

**$x = $y** would assign value of variable y to the variable x

**$x+=$y** is equal to $x = $x+$y

**$x-=$y** is equal to $x = $x-$y

**$x*=$y** is equal to $x = $x*$y

**$x/=$y** is equal to $x = $x/$y

**$x%=$y** is equal to $x = $x%$y

**$x**=$y** is equal to $x = $x**$y

#### Example:

#!/usr/local/bin/perl $x = 5; $result = 10; print "\$x= $x and \$result=$result\n"; $result = $x; print '= Operator output: ' . $result . "\n"; print "\$x= $x and \$result=$result\n"; $result += $x; print '+= Operator output: ' . $result . "\n"; print "\$x= $x and \$result=$result\n"; $result -= $x; print '-= Operator output: ' . $result . "\n"; print "\$x= $x and \$result=$result\n"; $result *= $x; print '*= Operator output: ' . $result . "\n"; print "\$x= $x and \$result=$result\n"; $result /= $x; print '/= Operator output: ' . $result . "\n"; print "\$x= $x and \$result=$result\n"; $result %= $x; print '%= Operator output: ' . $result . "\n"; #assigning different value to $result for this operator $result =2; print "\$x= $x and \$result=$result\n"; $result **= $x; print '**= Operator output: ' . $result . "\n";

**Output:**

$x= 5 and $result=10 = Operator output: 5 $x= 5 and $result=5 += Operator output: 10 $x= 5 and $result=10 -= Operator output: 5 $x= 5 and $result=5 *= Operator output: 25 $x= 5 and $result=25 /= Operator output: 5 $x= 5 and $result=5 %= Operator output: 0 $x= 5 and $result=2 **= Operator output: 32

## 3) Auto-increment and Auto-decrement Operators

++ and —

**$x++** is equivalent to $x=$x+1;

**$x–** is equivalent to $x=$x-1;

#### Example:

#!/usr/local/bin/perl $x =100; $y =200; $x++; $y--; print"Value of \$x++ is: $x\n"; print"Value of \$y-- is: $y\n";

**Output:**

Value of $x++ is: 101 Value of $y-- is: 199

## 4) Logical Operators

Logical Operators are used with binary variables. They are mainly used in conditional statements and loops for evaluating a condition.

Logical operators in perl are: &&, and, ||, or, not, !

“**&&**” and “**and**” are same

$x&&y will return true if both x and y are true else it would return false.

“**||**” and “**or**” are same.

$x||$y will return false if both x and y are false else it would return true.

“**!**” and “**not**” are same.

!$x would return the opposite of x, that means it would be true if x is false and it would return false if x is true.

#### Example

#!/usr/local/bin/perl $x = true; $y = false; $result = ($x and $y); print"\$x and \$y: $result\n"; $result = ($x && $y); print"\$x && \$y: $result\n"; $result = ($x or $y); print"\$x or \$y: $result\n"; $result = ($x || $y); print"\$x || \$y: $result\n"; #point to note is that not operator works #with 0 and 1 only. $x=0; $result = not($x); print"not\$x: $result\n"; $result = !($x); print"\!\$x: $result\n";

**Output:**

$x and $y: false $x && $y: false $x or $y: true $x || $y: true not$x: 1 !$x: 1

## 5) Comparison(Relational) operators

They includes: ==, eq, !=, ne, >, gt, <, lt, >=, ge, <=, le

== and eq returns true if both the left side and right side are equal

!= and ne returns true if left side is not equal to the right side of operator.

**>** and **gt** returns true if left side is greater than right.

**<** and **lt** returns true if left side is less than right side.

**>=** and **ge** returns true if left side is greater than or equal to right side.

**<=** and **le** returns true if left side is less than or equal to right side.

#### Example

#!/usr/local/bin/perl $x = 3; $y = 6; if( $x == $y ){ print "\$x and \$y are equal\n"; }else{ print "\$x and \$y are not equal\n"; } if( $x != $y ){ print "\$x and \$y are not equal\n"; }else{ print "\$x and \$y are equal\n"; } if( $x > $y ){ print "\$x is greater than \$y\n"; }else{ print "\$x is not greater than \$y\n"; } if( $x >= $y ){ print "\$x is greater than or equal to \$y\n"; }else{ print "\$x is less than \$y\n"; } if( $x < $y ){ print "\$x is less than \$y\n"; }else{ print "\$x is not less than \$y\n"; } if( $x <= $y){ print "\$x is less than or equal to \$y\n"; }else{ print "\$x is greater than \$y\n"; }

**Output:**

$x and $y are not equal $x and $y are not equal $x is not greater than $y $x is less than $y $x is less than $y $x is less than or equal to $y

## 6) Bitwise Operators

There are six bitwise Operators: &, |, ^, ~, <<, >>

$x = 11; #00001011

$y = 22; #00010110

Bitwise operator performs bit by bit processing.

**$x & $y** compares corresponding bits of x and y and generates 1 if both bits are equal, else it returns 0.

In our case it would return: 2 which is 00000010 because in the binary form of x and y only second last bits are matching.

**$x | $y** compares corresponding bits of x and y and generates 1 if either bit is 1, else it returns 0.

In our case it would return 31 which is 00011111

**$x ^ $y** compares corresponding bits of x and y and generates 1 if they are not equal, else it returns 0.

In our example it would return 29 which is equivalent to 00011101

**~$x** is a complement operator that just changes the bit from 0 to 1 and 1 to 0

In our example it would return -12 which is signed 8 bit equivalent to 11110100

**<<** is left shift operator that moves the bits to the left, discards the far left bit, and assigns the rightmost bit a value of 0.

In out case output is 44 which is equivalent to 00101100

**Note:** In the example below we are providing 2 at the right side of this shift operator that is the reason bits are moving two places to the left side. We can change this number and bits would be moved by the number of bits specified on the right side of the operator. Same applies to the right side operator.

**>>** is right shift operator that moves the bits to the right, discards the far right bit, and assigns the leftmost bit a value of 0.

in our case output is 2 which is equivalent to 00000010

#### Example:

#!/usr/local/bin/perl use integer; $x = 11; #00001011 $y = 22; #00010110 $result = $x & $y; print "\$x & \$y: $result\n"; $result = $x | $y; print "\$x | \$y: $result\n"; $result = $x ^ $y; print "\$x ^ \$y: $result\n"; $result = ~$x; print "~\$x = $result\n"; $result = $x << 2; print "\$x << 2 = $result\n"; $result = $x >> 2; print "\$x >> 2 = $result\n";

**Output:**

$x & $y: 2 $x | $y: 31 $x ^ $y: 29 ~$x = -12 $x << 2 = 44 $x >> 2 = 2

## 7) Quote and Quote-like Operators

There are several quote like operators in perl like q{ }, qq{ }, qw{ }, m{ }, qr{ }, s{ }{ }, tr{ }{ }, y{ }{ }.

However only two of them are frequently used: q{ } is for single quote and qq{ } is for double quote.

#### Example

q{Welcome to beginnersbook} would return ‘Welcome to beginnersbook’

qq{Welcome to beginnersbook} would return “Welcome to beginnersbook”

## Leave a Reply