Previous
Variables and Arrays
CGI-Perl Tutorial
Operators
Next
Control Flow - If

Operators

There is no lack of operators in perl. Only the most important and the most used are listed below.

Note : The variable $a has the value 3 and $b has the value 8. These values will NOT change - in other words, these values will be same for every example.

Arithmetic operators
OperatorExplanationExampleExample Result
+addition$a + $b11
-subtraction5 - $a2
*multiplication3 * $b24
/division6 / $a2


Extras
OperatorExplanationExampleExample Result
++Adds 1 to the value on the left (so if $i were equal to 1, $i++ would equal 2)$a++$a will be 4
--Subtracts 1 from the value on the left.$a--$a will be
+=Adds the values to the left and right of the operator and then stores the value in the left variable. $a += $b;$a will be 11
-=Subtracts the values to the left and right of the operator and then stores the value in the left variable. $b -= $a;$b will be 5
*=Multiplies the values to the left and right of the operator and then stores the value in the left variable. $a *= $b;$a will be 24


Numeric comparison
OperatorExplanationExampleExample Result
==equalityif($a == $b)False
!=inequalityif($a != $b)True
<less thanif($a < $b)True
>greater thanif($a > $b)False
<=less than or equalif($a <= $b)True
>=greater than or equalif($a >= $b)False


String comparison
OperatorExplanationExampleExample Result
eqSee whether two strings are equal. "String" eq "String"True
neSee whether two strings are NOT equal. "String" ne "String"False


Boolean logic
OperatorExplanationExample Example Result
&&AND if($a>$b && $b>7)False
||OR if($a>$b || $b>7)True
!NOT if(!$b)False


Other operators
OperatorExplanationExampleExample Result
=assignment $a = 5;$a will become 5
.string concatenation(In case you can't see the operator, its is a dot - a period - '.') $a = "Str1" . "Str2";$a will be "Str1Str2"
xstring multiplication $a = "Me" x 5$a will be "MeMeMeMeMe"

Most of the operators for perl are common among the popular languages like C++, Java etc. So if you have any programming experience, you will be well at ease with the operators in perl. But there are a few additions in perl like 'eq', 'ne', '.' etc. Take a moment to learn them and then let us move on....


Previous
Variables and Arrays
Contents Next
Control Flow - If