Operators

Let us understand the below terms before we get into more details.

1. Operator

An operator is a symbol which has special meaning and performs an operation on single or multiple operands like addition, substraction etc. In the below example, + is the operator.

$x = 10;
$y = 20;
$sum = $x + $y;
print($sum);

Check result here

2. Operand

An operand is what operators are applied on. In the above example x and y are the operands.

Types of Operators in Perl

1. Arithmetic Operators

Perl arithmetic operators are used to perform arithmetic operations on operands.

OperatorDescriptionExample
+Used to perform Addition8+2 = 10
-Used to perform Subtraction12-2 = 10
*Used to perform Multiplication5*2 = 10
/Used to perform Division100/10 = 10
%Used to return Remainder40%10 = 0
**Used to perform Exponentation10**2 means 10 to the power of 2

Example

   $x = 90;
   $y = 10;
 
   $sum = $x + $y;
   print ("Sum : " , $sum ,"\n");
   
   $diff = $x - $y;
   print ( "Difference : " , $diff  ,"\n");
   
   $product = $x * $y;
   print ( "Product : " , $product  ,"\n");
   
   $division = $x / $y;
   print ( "Division : " , $division  ,"\n");
   
   $mod = $x % $y;
   print ( "Remainder : " , $mod  ,"\n");
   
   $inc = $x++;
   print ( "x value after incrementing : " , $x  ,"\n");
   
   $dec = $x--;
   print ( "x value after decrementing : " , $x ,"\n");
 
   $pow = $y ** 2;
   print ( "$y to the power of 2 : " , $pow  ,"\n");
   

Check Result here

2. Equality Operators

Equality operators are used to compare two operands.

OperatorDescriptionUsage
==Is equal tox == y
!=Not equal to!=x
>Greater thanx > y
>=Greater than or equal tox >= y
<Less thanx < y
<=Less than or equal tox <= y

Example

   $x = 90;
   $y = 10;
   
  if ( $x == $y) {
    print "x and y are equal \n";
  }
  
  if ( $x != $y) {
    print "x and y are not equal \n";
  }
  
  if ( $x > $y) {
    print "x is greater than y \n";
  }
  
  if ( $x < $y) {
    print "x is less than y \n";
  }

Check Result here

3. Bitwise Operators

Bitwise operators are used to perform bitwise operations on operands.

OperatorDescriptionUsage
&Bitwise AND($x > $y) & ($y > $z)
``Bitwise OR
^Bitwise $xOR($x > $y) ^ ($y > $z)
~Bitwise NOT(~$x)
<<Bitwise Left Shift$x << $y
>>Bitwise Right Shift$x >> $y

4. Logical operators

Below are the logical operators present in Perl.

OperatorDescriptionUsage
&&Logical AND($x > $y) && ($y > $z)
``Logical OR
!Logical NOT!($x)

5. Assignment Operators

Below are the assignment operators present in Perl.

OperatorDescriptionUsage
=Assign$x = 10;
+=Add and assignx=10;x=10; x+=30; # x becomes 40
-=Subtract and assign$x=40; x-=10; # x becomes 30
*=Multiply and assign$x=10; x*=40; # x becomes 400
/=Divide and assignx=100;x=100; x /= 10;# x becomes 10
%=Modulus and assignx=100;x=100; x%=10; # x becomes 0
<<=Left shift and assignx<<=2issameasx <<= 2 is same as x = $x << 2
>>=Right shift and assignx>>=2issameasx >>= 2 is same as x = $x >> 2
&=Bitwise and assignx &= 10 is same as x = $x & 10
^=Bitwise exclusive OR and assignx=10issameasx ^= 10 is same as x = $x ^ 10
|=Bitwise inclusive OR and assign`$x

Example

$x = 10; #assigning 10 to x 
print "x value: $x \n ";
        
$x+=30;
print "x value after += operation: $x \n ";
        
$x-=10;
print "x value after -= operation: $x \n ";
        
$x*=10;
print "x value after *= operation: $x \n ";
        
$x/=10;
print "x value after /= operation: $x \n ";
        
$x%=10;
print "x value after %= operation: $x";   

Check Result here

6. Quote like operators

OperatorDescriptionExample
q{ }Encloses the given string within single quotesq{One Compiler} is same as 'One Compiler'
qq{ }Encloses the given string within double quotesqq{One Compiler} is same as "One Compiler"
qx{ }Encloses the given string within invert quotesqx{One Compiler} is same as One Compiler