An operator is a symbol that tells the compiler to perform specific mathematical or logical operations, for example: 3+2=5.
\n\nPerl has a rich set of built-in operators. Let's look at some of the common ones:
\n\n- \n
- Arithmetic Operators \n
- Comparison Operators \n
- Logical Operators \n
- Assignment Operators \n
- Bitwise Operators \n
- Quote Operators \n
- Miscellaneous Operators \n
- Operator Precedence \n
\n\n
Arithmetic Operators
\n\nIn the table examples, we set the variable $a to 10 and $b to 20.
\n\n| Operator | \nDescription | \nExample | \n
|---|---|---|
| + | \nAddition | \n$a + $b results in 30 | \n
| - | \nSubtraction | \n$a - $b results in -10 | \n
| * | \nMultiplication | \n$a * $b results in 200 | \n
| / | \nDivision | \n$b / $a results in 2 | \n
| % | \nModulus (remainder of integer division) | \n$b % $a results in 0 | \n
| ** | \nExponentiation | \n$a**$b results in 10 to the power of 20 | \n
Example
\n\n#!/usr/bin/perl\n$a = 10;\n$b = 20;\nprint "$a = $a , $b = $bn";\n$c = $a + $b;\nprint '$a + $b = ' . $c . "n";\n$c = $a - $b;\nprint '$a - $b = ' . $c . "n";\n$c = $a * $b;\nprint '$a * $b = ' . $c . "n";\n$c = $a / $b;\nprint '$a / $b = ' . $c . "n";\n$c = $a % $b;\nprint '$a % $b = ' . $c . "n";\n$a = 2;\n$b = 4;\n$c = $a ** $b;\nprint '$a ** $b = ' . $c . "n";\n\nThe output of the above program is:
\n\n$a = 10 , $b = 20\n$a + $b = 30\n$a - $b = -10\n$a * $b = 200\n$a / $b = 0.5\n$a % $b = 10\n$a ** $b = 16\n\n\n\n
Comparison Operators
\n\nIn the table examples, we set the variable $a to 10 and $b to 20.
\n\n| Operator | \nDescription | \nExample | \n
|---|---|---|
| == | \nChecks if the values of two operands are equal. If yes, the condition is true, otherwise false. | \n($a == $b) is false. | \n
| != | \nChecks if the values of two operands are not equal. If yes, the condition is true, otherwise false. | \n($a != $b) is true. | \n
| <=> | \nChecks if the values of two operands are equal. Returns -1 if the left operand is less than the right, 0 if equal, and 1 if the left operand is greater than the right. | \n($a <=> $b) returns -1. | \n
| > | \nChecks if the value of the left operand is greater than the right. If yes, the condition is true, otherwise false. | \n($a > $b) returns false. | \n
| < | \nChecks if the value of the left operand is less than the right. If yes, the condition is true, otherwise false. | \n($a < $b) returns true. | \n
| >= | \nChecks if the value of the left operand is greater than or equal to the right. If yes, the condition is true, otherwise false. | \n($a >= $b) returns false. | \n
| <= | \nChecks if the value of the left operand is less than or equal to the right. If yes, the condition is true, otherwise false. | \n($a <= $b) returns true. | \n
Example
\n\n#!/usr/bin/perl\n$a = 10;\n$b = 20;\nprint "$a = $a , $b = $bn";\nif($a == $b){\n print "$a == $b Result truen";\n}else{\n print "$a == $b Result false";\n}\nif($a != $b){\n print "$a != $b Result truen";\n}else{\n print "$a != $b Result false";\n}\n$c = $a <=> $b;\nprint "$a <=> $b Return $cn";\nif($a > $b){\n print "$a > $b Result truen";\n}else{\n print "$a > $b Result false";\n}\nif($a >= $b){\n print "$a >= $b Result truen";\n}else{\n print "$a >= $b Result false";\n}\nif($a < $b){\n print "$a < $b Result truen";\n}else{\n print "$a < $b Result false";\n}\nif($a <= $b){\n print "$a <= $b Result truen";\n}else{\n print "$a <= $b Result false";\n}\n\nThe output of the above program is:
\n\n$a = 10 , $b = 20\n$a == $b Result false\n$a != $b Result true\n$a <=> $b Return -1\n$a > $b Result false\n$a >= $b Result false\n$a < $b Result true\n$a <= $b Result true\n\nIn the following table examples, we set the variable $a to "abc" and $b to "xyz", then use comparison operators to calculate the results.
\n\n| Operator | \nDescription | \nExample | \n
|---|---|---|
| lt | \nChecks if the left string is less than the right. If yes, returns true, otherwise false. | \n($a lt $b) returns true. | \n
| gt | \nChecks if the left string is greater than the right. If yes, returns true, otherwise false. | \n($a gt $b) returns false. | \n
| le | \nChecks if the left string is less than or equal to the right. If yes, returns true, otherwise false. | \n($a le $b) returns true. | \n
| ge | \nChecks if the left string is greater than or equal to the right. If yes, returns true, otherwise false. | \n($a ge $b) returns false. | \n
| eq | \nChecks if the left string is equal to the right. If yes, returns true, otherwise false. | \n($a eq $b) returns false. | \n
| ne | \nChecks if the left string is not equal to the right. If yes, returns true, otherwise false. | \n($a ne $b) returns true. | \n
| cmp | \nReturns 1 if the left string is greater than the right, 0 if equal, and -1 if the left string is less than the right. | \n($a cmp $b) returns -1. | \n
Example
\n\n#!/usr/bin/perl\n$a = "abc";\n$b = "xyz";\nprint "$a = $a ๏ผ$b = $bn";\nif($a lt $b){\n print "$a lt $b Return truen";\n}else{\n print "$a lt $b Return falsen";\n}\nif($a gt $b){\n print "$a gt $b Return truen";\n}else{\n print "$a gt $b Return falsen";\n}\nif($a le $b){\n print "$a le $b Return truen";\n}else{\n print "$a le $b Return falsen";\n}\nif($a ge $b){\n print "$a ge $b Return truen";\n}else{\n print "$a ge $b Return falsen";\n}\nif($a ne $b){\n print "$a ne $b Return truen";\n}else{\n print "$a ne $b Return falsen";\n}\n$c = $a cmp $b;\nprint "$a cmp $b Return $cn";\n\nThe output of the above program is:
\n\n$a = abc ๏ผ$b = xyz\nabc lt $b Return true\n$a gt $b Return false\n$a le $b Return true\n$a ge $b Return false\n$a ne $b Return true\n$a cmp $b Return -1\n\n\n\n
Assignment Operators
\n\nIn the table examples, we set the variable $a to 10 and $b to 20.
\n\n| Operator | \nDescription | \nExample | \n
|---|---|---|
| = | \nSimple assignment operator. Assigns the value from the right operand to the left operand. | \n$c = $a + $b assigns the value of $a + $b to $c | \n
| += | \nAdd and assignment operator. Adds the right operand to the left operand and assigns the result to the left operand. | \n$c += $a is equivalent to $c = $c + $a | \n
| -= | \nSubtract and assignment operator. Subtracts the right operand from the left operand and assigns the result to the left operand. | \n$c -= $a is equivalent to $c = $c - $a | \n
| *= | \nMultiply and assignment operator. Multiplies the right operand with the left operand and assigns the result to the left operand. | \n$c *= $a is equivalent to $c = $c * $a | \n
| /= | \nDivide and assignment operator. Divides the left operand by the right operand and assigns the result to the left operand. | \n$c /= $a is equivalent to $c = $c / $a | \n
| %= | \nModulus and assignment operator. Takes modulus using two operands and assigns the result to the left operand. | \n$c %= $a is equivalent to $c = $c % a | \n
| **= | \nExponentiation and assignment operator. Raises the left operand to the power of the right operand and assigns the result to the left operand. | \n$c **= $a is equivalent to $c = $c ** $a | \n
Example
\n\n#!/usr/bin/perl\n$a = 10;\n$b = 20;\nprint "$a = $a ๏ผ$b = $bn";\n$c = $a + $b;\nprint "After assignment $c = $cn";\n$c += $a;\nprint "$c = $c ๏ผExpression statement $c += $an";\n$c -= $a;\nprint "$c = $c ๏ผExpression statement $c -= $an";\n$c *= $a;\nprint "$c = $c ๏ผExpression statement $c *= $an";\n$c /= $a;\nprint "$c = $c ๏ผExpression statement $c /= $an";\n$c %= $a;\nprint "$c = $c ๏ผExpression statement $c %= $an";\n$c = 2;\n$a = 4;\nprint "$a = $a ๏ผ $c = $cn";\n$c **= $a;\nprint "$c = $c ๏ผExpression statement $c **= $an";\n\nThe output of the above program is:
\n\n$a = 10 ๏ผ$b = 20\nAfter assignment $c = 30\n$c = 40 ๏ผExpression statement $c += $a\n$c = 30 ๏ผExpression statement $c -= $a\n$c = 300 ๏ผExpression statement $c *= $a\n$c = 30 ๏ผExpression statement $c /= $a\n$c = 0 ๏ผExpression statement $c %= $a\n$a = 4 ๏ผ $c = 2\n$c = 16 ๏ผExpression statement $c **= $a\n\n\n\n
Bitwise Operators
\n\nBitwise operators work on bits and perform bit-by-bit operations.
\n\nSet $a = 60, $b = 13. In binary format, they are shown as follows:
\n\n$a = 0011 1100\n$b = 0000 1101\n-----------------\n$a & $b = 0000 1100\n$a | $b = 0011 1101\n$a ^ $b = 0011 0001\n~$a = 1100 0011\n\nPerl supports the following bitwise operators:
\n\n| Operator | \nDescription | \nExample | \n
|---|---|---|
| & | \nBinary AND operator. Copies a bit to the result if it exists in both operands. | \n($a & $b) will give 12, binary 0000 1100 | \n
| | | \nBinary OR operator. Copies a bit to the result if it exists in either operand. | \n($a | $b) will give 61, binary 0011 1101 | \n
| ^ | \nBinary XOR operator. Copies a bit to the result if it exists in one operand but not both. | \n($a ^ $b) will give 49, binary 0011 0001 | \n
| ~ | \nBinary Ones Complement operator. A unary operator that "flips" bits, i.e., 0 becomes 1 and 1 becomes 0. | \n(~$a) will give -61, binary 1100 0011, which is the two's complement form of a signed binary number. | \n
| << | \nBinary Left Shift operator. The left operand's value is shifted left by the number of bits specified by the right operand. | \n$a << 2 will give 240, binary 1111 0000 | \n
| >> | \nBinary Right Shift operator. The left operand's value is shifted right by the number of bits specified by the right operand. | \n$a >> 2 will give 15, binary 0000 1111 | \n
Example
\n\n#!/usr/bin/perl\nuse integer;\n$a = 60;\n$b = 13;\nprint "$a = $a , $b = $bn";\n$c = $a & $b;\nprint "$a & $b = $cn";\n$c = $a | $b;\nprint "$a | $b = $cn";\n$c = $a ^ $b;\nprint "$a ^ $b = $cn";\n$c = ~$a;\nprint "~$a = $cn";\n$c = $a << 2;\nprint "$a << 2 = $cn";\n$c = $a >> 2;\nprint "$a >> 2 = $cn";\n\nThe output of the above program is:
\n\n$a = 60 , $b = 13\n$a & $b = 12\n$a | $b = 61\n$a ^ $b = 49\n~$a = -61\n$a << 2 = 240\n$a >> 2 = 15\n\n\n\n
Logical Operators
\n\nPerl logical operators are shown in the following table.
\n\nIn the table examples, we set the variable $a to true and $b to false.
\n\n| Operator | \nDescription | \nExample | \n
|---|---|---|
| and | \nLogical AND operator. If both operands are true, the condition is true. | \n($a and $b) is false. | \n
| && | \nC-style Logical AND operator. If both operands are true, the condition is true. | \n($a && $b) is false. | \n
| or | \nLogical OR operator. If any of the two operands is non-zero, the condition is true. | \n($a or $b) is true. | \n
| || | \nC-style Logical OR operator. If any of the two operands is non-zero, the condition is true. | \n($a || $b) is true. | \n
| not | \nLogical NOT operator. Used to reverse the logical state of its operand. If the condition is true, the logical NOT operator will make it false. | \nnot($a and $b) is true. | \n
Example
\n\n#!/usr/bin/perl\n$a = true;\n$b = false;\nprint "$a = $a , $b = $bn";\n$c = ($a and $b);\nprint "$a and $b = $cn";\n$c = ($a && $b);\nprint "$a && $b = $cn";\n$c = ($a or $b);\nprint "$a or $b = $cn";\n$c = ($a || $b);\nprint "$a || $b = $cn";\n$a = 0;\n$c = not($a);\nprint "not($a)= $cn";\n\nThe output of the above program is:
\n\n$a = true , $b = false\n$a and $b = false\n$a && $b = false\n$a or $b = true\n$a || $b = true\nnot($a)= 1\n\n\n\n
Quote Operators
\n\nPerl quote operators are shown in the following table.
\n\n| Operator | \nDescription | \nExample | \n
|---|---|---|
| q{ } | \nAdds single quotes to a string. | \nq{abcd} results in 'abcd' | \n
| qq{ } | \nAdds double quotes to a string. | \nqq{abcd} results in "abcd" | \n
| qx{ } | \nAdds backticks to a string. | \nqx{abcd} results in `abcd` | \n
Example
\n\n#!/usr/bin/perl\n$a = 10;\n$b = q{a = $a};\nprint "q{a = $a} = $bn";\n$b = qq{a = $a};\nprint "qq{a = $a} = $bn";\n$t = qx{date};\nprint "qx{date} = $tn";\n\nThe output of the above program is:
\n\nq{a = $a} = a = $a\nqq{a = $a} = a = 10\nqx{date} = 2016Friday, June 10, 16:22:33 CST\n\n\n\n
Miscellaneous Operators
\n\nIn addition to the operators mentioned above, Perl also supports the following operators:
\n\n| Operator | \nDescription | \nExample | \n
|---|---|---|
| . | \nThe dot (.) is used to concatenate two strings. | \nIf $a="run", $b="oob", then $a.$b results in "" | \n
| x | \nThe x operator returns the string repeated a specified number of times. | \n('-' x 3) outputs ---. | \n
| .. | \nThe .. is the range operator. | \n(2..5) outputs (2, 3, 4, 5) | \n
| ++ | \nIncrement operator. Increases an integer value by 1. | \n$a = 10, $a++ outputs 11 | \n
| -- | \nDecrement operator. Decreases an integer value by 1. | \n$a = 10, $a-- outputs 9 | \n
| -> | \nThe arrow is used to specify a method of a class. | \n$obj->$a denotes the $a method of object $obj. | \n
Example
\n\n#!/usr/bin/perl\n$a = "run";\n$b = "oob";\nprint "$a = $a ๏ผ $b = $bn";\n$c = $a . $b;\nprint "$a . $b = $cn";\n$c = "-" x 3;\nprint ""-" x 3 = $cn";\n@c = (2..5);\nprint "(2..5) = @cn";\n$a = 10;\n$b = 15;\nprint "$a = $a ๏ผ $b = $bn";\n$a++;\n$c = $a;\nprint "$a Execute $a++ = $cn";\n$b--;\n$c = $b;\nprint "$b Execute $b-- = $cn";\n\nThe output of the above program is:
\n\n$a = run ๏ผ $b = oob\n$a . $b = \n"-" x 3 = ---\n(2..5) = 2 3 4 5\n$a = 10 ๏ผ $b = 15\n$a Execute $a++ = 11\n$b Execute $b-- = 14\n\n\n\n
Operator Precedence
\n\nThe following table lists the operator precedence in Perl:
\n\n| Operator | \nAssociativity | \n
|---|---|
| ++,-- | \nNon-associative | \n
| -,~,! | \nRight to left | \n
| ** | \nRight to left | \n
| =~,!~ | \nLeft to right | \n
| *,/,%,x | \nLeft to right | \n
| +,-,. | \nLeft to right | \n
| <<,>> | \nLeft to right | \n
| -e,-r, | \nNon-associative | \n
| <,<=,>,>=,lt,le,gt,ge | \nLeft to right | \n
| ==,!=,<=>,eq,ne,cmp | \nLeft to right | \n
| & | \nLeft to right | \n
| |,^ | \nLeft to right | \n
| && | \nLeft to right | \n
| || | \nLeft to right | \n
| .. | \nLeft to right | \n
| ? and : | \nRight to left | \n
| =,+=,-=,*=, | \nRight to left | \n
| Others | \n\n |
| , | \nLeft to right | \n
| not | \nLeft to right | \n
| and | \nLeft to right | \n
| or,xor | \nLeft to right | \n
Example
\n\n#!/usr/bin/perl\n$a = 20;\n$b = 10;\n$c = 15;\n$d = 5;\n$e;\nprint "$a = $a, $b = $b, $c = $c ๏ผ$d = $dn";\n$e = ($a + $b) * $c / $d;\nprint "($a + $b) * $c / $d = $en";\n$e = (($a + $b) * $c) / $d;\nprint "(($a + $b) * $c) / $d = $en";\n$e = ($a + $b) * ($c / $d);\nprint "($a + $b) * ($c / $d ) = $en";\n$e = $a + ($b * $c) / $d;\nprint "$a + ($b * $c )/ $d = $en";\n\nThe output of the above program is:
\n\n$a = 20, $b = 10, $c = 15 ๏ผ$d = 5\n($a + $b) * $c / $d = 90\n(($a + $b) * $c) / $d = 90\n($a + $b) * ($c / $d ) = 90\n$a + ($b * $c )/ $d = 50
YouTip