| Previous Variables and Arrays |
CGI-Perl Tutorial Operators |
Next Control Flow - If |
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| Operator | Explanation | Example | Example Result |
| + | addition | $a + $b | 11 |
| - | subtraction | 5 - $a | 2 |
| * | multiplication | 3 * $b | 24 |
| / | division | 6 / $a | 2 |
| Operator | Explanation | Example | Example 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 |
| Operator | Explanation | Example | Example Result |
| == | equality | if($a == $b) | False |
| != | inequality | if($a != $b) | True |
| < | less than | if($a < $b) | True |
| > | greater than | if($a > $b) | False |
| <= | less than or equal | if($a <= $b) | True |
| >= | greater than or equal | if($a >= $b) | False |
| Operator | Explanation | Example | Example Result |
| eq | See whether two strings are equal. | "String" eq "String" | True |
| ne | See whether two strings are NOT equal. | "String" ne "String" | False |
| Operator | Explanation | Example | Example Result |
| && | AND | if($a>$b && $b>7) | False |
| || | OR | if($a>$b || $b>7) | True |
| ! | NOT | if(!$b) | False |
| Operator | Explanation | Example | Example 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" |
| x | string 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 |