Perl Programming - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Perl Opearator

Perl Opearator

shape Introduction

This chapter demonstrates about the Perl Opearator which is an action to performed on some set of variables or values which is understandable by the computer and following are the concepts covered in this chapter.
  • Operators

Operators

shape Description

Operators in computer language indicate an action which can be performed on some set of variables or values i.e understandable by the computer. Perl has incorporated most of the Operators from C language. Perl has several sets of operators compared with other programming languages. Operators are classified as Arithmetic, Logical, relational and assignment operators. Arithmetic Operators Arithmetic operators are those which might be used to perform some basic mathematic operations. These Arithmetic operators are binary operators where a user need two arguments to perform a basic operation. we can also use single operators for other basic operations The table below demonstrates the some Arithmetic operators are shown below. The code below demonstrates the Different types of Arithmetic operators are shown below. [perl] my $x=10; my $y=2; my $z; $z=$x+$y; print ("Add of $x and $y is $z <br/>"); $z=$x-$y; print ("Sub of $x and $y is $z <br/>"); $z=$x*$y; print ("Mul of $x and $y is $z <br/>"); $z=$x/$y; print ("Div of $x and $y is $z <br/>"); $z=$x**$y; print ("Exp of $x and $y is $z <br/>"); $z=$x%$y; print ("Mod of $x and $y is $z <br/>"); [/perl] Result By running the above code in a Perl command line user can get the following output as shown in below image. Assignment Operators Assignment operators simply assign values to variables, however, there is one more issue which User need to remember i.e operators will also perform arithmetic operations and assign the new value to the same variable on which the operation is performed. The code below demonstrates the Different types of Assignment operators are shown below. [perl] my $x=10; $x+=5; print("Add = $x<br/>"); $x-=5; print("Sub= $x<br/>"); $x*=5; print("Mul = $x<br/>"); $x/=5; print("Div = $x<br/>"); [/perl] Result By running the above code in a Perl command line user can get the following output as shown in below image. Logical and Relational Operators Mostly Perl uses logical operators to compare numbers and strings. Most of the time logical operators are used in Conditional Statements. The image below demonstrates the Comparision of logical operators. The code below demonstrates the Different types of Assignment operators are shown below. [perl] my $x=5; my $y=5; if($x == $y){ print ("True -- equal $x and $y <br/>"); } else{ print ("False -- not equal $x and $y<br/>"); } $x=6; $y=7; if($x != $y){ print ("True -- not equal $x and $y<br/>"); } else{ print ("False -- equal $x and $y<br/>"); } if($y > $x){ print ("True -- $y greater than $x<br/>"); } else{ print ("False -- $y greater than $x<br/>"); } if($x < $y){ print ("True -- $x less than $y<br/>"); } else{ print ("False -- $x less than $y<br/>"); } if($x <= $y){ print ("True -- $x less than $y<br/>"); } else{ print ("False -- $x less than $y<br/>"); } if($y >= $x){ print ("True -- $y greater than $x<br/>"); } else{ print ("False -- $y greater than $x<br/>"); } [/perl] Result By running the above code in a Perl command line user can get the following output as shown in below image.

Summary

shape Key Points

  • Logical operators used to compare numbers and strings.
  • Assignment operators simply assign values to variable.
  • Arithamatic operations used to perform some basic operations.