# Begin Perl Programming with Perl Operators Operators 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:

• Arithmetic Operators
• Equality Operators
• Logical Operators
• Assignment Operators
• Bitwise Operators
• Quote-like Operators

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:

•           – Subtraction

•           * Multiplication

•           / Division

•           % Modulo Division

Example of Arithmetic Operators:

```#!/usr/bin/perl

# Calculations

\$x = 70;

\$y=9;

\$sub = \$x - \$y;

\$mul = \$x * \$y;

\$div = \$x / \$y;

\$exp = \$x ** \$y;

\$mod = \$x % \$y;

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:

• <is less than
• <= is less than or equal to
• >is greater than
• >= is greater than or equal to
• == is equal to
• != not equal to
• <=>comparison operator

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

• eq(==): 5==5 or 5 eq 5 are same
• ne(!=): 8!=2 or 8 ne 2 are same
• lt(<): 7<4 or 7 lt 4 are same
• gt(>): 8>4 or 8 gt 4 are same
• le(<=): 7<=12 or 7 le 12 are same
• ge(>=): 7 >=12 or 7 ge 12 are same
• cpm : It is a compare operator that returns either -1, 0 or 1 when compared with two strings. It returns -1 when the first argument is small and 0 when both the arguments are equal. Similarly, it returns 1 when the first argument is greater.

## 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.

• &&,and: Returns true when both condition are true.
• Example (4<5 && 5>4) will return true.
• ||,or: Returns true when any one or both conditions are true.
• not: Complements the value passed as argument.

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

• = : Assigns the value from right to left
• +=: Add and assignment operator. Adds left operand to right and stores the result in left.
• =-: Subtract and assignment operator. Subtracts left operand to right and stores the result in left.
• *=: Multiply and assignment operator. Multiplies left operand to right and stores the result in left.
• /=:Divide and assignment operator. Divides left operand to right and stores the result in left
• %=: Modulus and assignment operator. Modulus of left operand to right and stores the result in left
• **=: Exponential and assignment operator. Calculates the power of left operand to right and stores the result in left

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:

• Binary AND (&): Copies a bit if the result exists in both operands
• Binary OR (|) : Copies a bit if the result exists in either of the operands.
• Binary XOR(^):  Copies a bit if sent in one operand.
• Binary Ones(~):  Complements the bit
• Binary Right Shift(>>): The value of left operand to right by specified number of bits
• Binary Left Shift(<<): The value of left operand to left by specified number of bits

## Quote Operators:

Perl supports the following quote operators:

• q{}: Encloses the given string in single quotes.
• qq{}: Encloses the given string in double quotes
• qx{}: Encloses the given string in invert quotes