perl operatorsOperators are the basic building blocks of any programming language. They are used to perform conditional checks and mathematical manipulations.

The various types of Operators in Perl are:

Learn basics of Perl programming by taking up a course at Udemy.com

Arithmetic Operators:

These operators are used to perform simple mathematical calculations. Some basic operators are:

•           + Addition

•           – Subtraction

•           * Multiplication

•           / Division

•           % Modulo Division

Example of Arithmetic Operators:

#!/usr/bin/perl
# Calculations
$x = 70;
$y=9;
$add = $x + $y;
$sub = $x - $y;
$mul = $x * $y;
$div = $x / $y;
$exp = $x ** $y;
$mod = $x % $y;
print "Sum is $add \n";
print "Difference is $sub \n";
print "Multiplication is $mul \n";
print "Division is $div  \n";
print "Exponent is $exp  \n";
print "Modulus is $mod  \n";

Output:

Sum is 79

Difference is 61

Multiplication is 630

Division is 7.77777777777778

Exponent is 4.0353607e+16

Modulus is 7

 

Learn Perl programming from scratch through this tutorial at Udemy.com

Equality or Relational Operators

These operators are used to perform a comparison between the two quantities and conditional checks. The operator always gives two values which is either zero for false or one for true.

Different operators are:

Instead of relational operator symbols we can also use the equivalent short hand.

Logical Operators

These operators are used to evaluate a combination of relational expression. Logical operators are used in combinations of relational operators to test one or more conditions and make decisions.

Example of Relational Operators:

#!/usr/bin/perl
use strict;
use warnings;
my $num1 = 5;
if ($num1 < 1) {
print "Less than one!\n";
} elsif ($num1 <= 2) {
print "Less than or equal to !\n";
} elsif ($num1 == 3) {
print "Equality Check \n";
} elsif ($num1 > 6) {
print "Greater than six!\n";
} elsif ($num1 >= 4) {
print "Greater than or equal to four!\n";
}
if ($num1 lt 1) {
print "Less than one!\n";
} elsif ($num1 le 2) {
print "Less than or equal to !\n";
} elsif ($num1 eq 3) {
print "Equality Check\n";
} elsif ($num1 gt 6) {
print "Greater than six!\n";
} elsif ($num1 ge 4) {
print "Greater than or equal to four!\n";
}

Output:

Greater than or equal to four!

Greater than or equal to four!

Example of Logic Operators:

$a = true;
$b = false;
print "Value of \$a = $a and value of \$b = $b\n";
$c = ($a and $b);
print "Result of \$a and \$b = $c\n";
$c = ($a  && $b);
print "Result of \$a && \$b = $c\n";
$c = ($a or $b);
print "Result of \$a or \$b = $c\n";

Output:

Value of $a = true and value of $b = false

Result of $a and $b = false

Result of $a && $b = false

Result of $a or $b = true

Result of $a || $b = true

Result of not($a)= 1

Assignment Operators:

These operators are used to assign values  to the variable.

Different Types of operators are

For Example Assignment Operators:

$a = 20;
$b = 30;
print "Value of \$a = $a and value of \$b = $b\n";
$c = $a + $b;
print "After assignment Result is \$c = $c\n";
$c += $a;
print "Result is \$c = $c after statement \$c += \$a\n";
$c -= $a;
print "Result is \$c = $c after statement \$c -= \$a\n";
$c *= $a;
print "Result is \$c = $c after statement \$c *= \$a\n";
$c /= $a;
print "Result is \$c = $c after statement \$c /= \$a\n";
$c %= $a;
print "Result is \$c = $c after statement \$c %= \$a\n";
$d = 4;
$a = 16;
print "Value of \$a = $a and value of \$d = $d\n";
$d **= $a;
print "Value of \$d = $d after statement \$d **= \$a\n";

Output:

 

Value of $a = 20 and value of $b = 30

After assignment Result is $c = 50

Result is $c = 70 after statement $c += $a

Result is $c = 50 after statement $c -= $a

Result is $c = 1000 after statement $c *= $a

Result is $c = 50 after statement $c /= $a

Result is $c = 10 after statement $c %= $a

Value of $a = 16 and value of $d = 4

Value of $d = 4294967296 after statement $d **= $a

 

Bitwise Operators

Perl also provides a facility to manipulate data at bit level. These operators are used to test the bits or shift them right or left. Bitwise operators cannot be applied to float and double i.e. decimal values.

Some bitwise operators are:

 Quote Operators:

 Perl supports the following quote operators:

Perl students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo