In this tutorial, various operators used in this language are discussed with examples. Let us have a look.
Arithmetic Operators
Usage of different arithmatic operators are shown in the following table.
| Operator | Usage |
|---|---|
| addition (+) | $a + $b |
| subtraction (-) | $a - $b |
| multiplication (*) | $a * $b |
| division (/) | $a / $b |
| modulus (%) | $a % $b |
| exponent (**) | $a ** $b |
Equality Operators
These operators are also known as relational operator. If the operands are numeric, these operators can be used as shown in the following table.
| Operator | Usage for numeric data | Usage for string data |
|---|---|---|
| eual to (==) | ($a == $b) | ($a eq $b) |
| not eual to (!=) | ($a != $b) | ($a ne $b) |
| comparison (<=>) | ($a <=> $b) | ($a cmp $b) |
| greater than (>) | ($a > $b) | ($a gt $b) |
| less than (<) | ($a < $b) | ($a lt $b) |
| greater than equals (>=) | ($a >= $b) | ($a ge $b) |
| less than equals (<=) | ($a <= $b) | ($a le $b) |
Assignment Operator
Assignment operator is used to assign a value to a variable. The
syntax of the operator usage is
$c = $a + $b
. The operator is also used with other arithmatic opeartos, like
$c += $a
.
Bitwise Operators
This operator works on bits and perform bit by bit operation. The
operations are bitwise
AND (&), OR (|), XOR (^), one's complement (~), left
shift (<<), right shift (>>)
.
Let us consider
a = 60
and
b = 13
. The binary representation of the variables are
$a = 0011 1100
and
$b = 0000 1101
. The bitwise operations can be performed on variables as shown
below.
Logical Operators
There are following logical operators supported by Perl language. The operators are used as follow:
| Operator | style1 | style2 |
|---|---|---|
| Logical AND | ($a and $b) | ($a && $b) |
| Logical OR | ($a or $b) | ($a || $b) |
Quote-like Operators
There are following Quote-like operators supported by Perl
language. In the following table, a
{}
represents any pair of delimiters you choose.
| Operator | description |
|---|---|
| q{pqrs} | a string within single quotes like 'pqrs' |
| qq{pqrs} | a string within double quotes like "pqrs" |
| qx{pqrs} | a string within inverted quotes like `pqrs` |
Miscellaneous Operators
There are following miscellaneous operators supported by Perl language.
| Operator | description | example |
|---|---|---|
| Binary operator dot (.) | concatenates two strings. | If $a = "abc", $b = "def" then $a.$b will give "abcdef" |
| Repetition (x) | returns a string consisting of the left operand repeated the number of times specified by the right operand. | ('-' x 3) will give --- |
| Range (..) | returns a list of values counting from the left value to the right value | (2..5) will give (2, 3, 4, 5) |
| Auto increment (++) | increases integer value by one | $a++ will give 11 if previous value is 10 |
| Auto decrement (--) | decreases integer value by one | $a-- will give 9 if previous value is 10 |
| Arrow (->) | mostly used in dereferencing a method or variable from an object or a class name | $obj->$a is used to access variable $a from object $obj. |

No comments:
Post a Comment
Note: Only a member of this blog may post a comment.