An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation, such as: 3+2=5.
The Perl language has a wealth of built-in operators. Let’s take a look at the commonly used ones:
arithmetic operators
comparison operator
Logical operators
assignment operator
Bit operators
quote operator
Other operators
operator precedence
In the table example, we set the variables $a to 10 and $b to 20.
operator | describe | Example |
---|---|---|
+ | Addition | $a + $b results in 30 |
- | subtraction operation | $a - $b results in -10 |
* | Multiplication | $a * $b results in 200 |
/ | Division operation | $b / $a results in 2 |
% | Find remainder operation, remainder after integer division | $b % $a results in 0 |
** | Multiply to power | The result of $a**$b is 10 raised to the 20th power |
The output result of the execution of the above program is:
$a = 10 , $b = 20$a + $b = 30$a - $b = -10$a * $b = 200$a / $b = 0.5$a % $b = 10$a ** $ b = 16
In the table example, we set the variables $a to 10 and $b to 20.
operator | describe | Example |
---|---|---|
== | Checks whether the values of two operands are equal, if equal then the condition is true, otherwise it is false. | ($a == $b) is false |
!= | Checks whether the values of the two operands are equal, if not the condition is true, otherwise it is false. | ($a != $b) is true. |
<=> | Checks whether the values of the two operands are equal, returning -1 if the number on the left is less than the number on the right, 0 if they are equal, and 1 if the number on the left is greater than the number on the right. | ($a <=> $b) returns -1. |
> | Checks whether the value of the left operand is greater than the value of the right operand, if so then the condition is true, otherwise it is false. | ($a > $b) returns false. |
< | Checks whether the value of the left operand is less than the value of the right operand, if so then the condition is true, otherwise it returns false. | ($a < $b) returns true. |
>= | Checks whether the value of the left operand is greater than or equal to the value of the right operand, if so then the condition is true, otherwise it returns false. | ($a >= $b) returns false. |
<= | Checks whether the value of the left operand is less than or equal to the value of the right operand, if so then the condition is true, otherwise it returns false. . | ($a <= $b) returns true. |
The output result of the execution of the above program is:
$a = 10 , $b = 20$a == $b The result is false$a != $b The result is true$a <=> $b Return -1$a > $b The result is false$a >= $b The result is false $a < $b results true$a <= $b results true
In the following table example, set the variable $a to "abc" and $b to "xyz", and then use comparison operators to calculate the result.
operator | describe | Example |
---|---|---|
lt | Checks whether the string on the left is smaller than the string on the right, if so it returns true, otherwise it returns false. | ($a lt $b) returns true. |
gt | Checks whether the string on the left is greater than the string on the right, if so it returns true, otherwise it returns false. | ($a gt $b) returns false. |
le | Checks whether the string on the left is less than or equal to the string on the right, if so it returns true, otherwise it returns false. | ($a le $b) returns true |
ge | Checks whether the string on the left is greater than or equal to the string on the right, if so it returns true, otherwise it returns false. | ($a ge $b) returns false. |
eq | Checks whether the string on the left is equal to the string on the right, if so it returns true, otherwise it returns false. | ($a eq $b) returns false. |
ne | Checks whether the string on the left is not equal to the string on the right, if so it returns true, otherwise it returns false. | ($a ne $b) returns true |
cmp | Returns 1 if the string on the left is greater than the string on the right, 0 if equal, and -1 if the string on the left is less than the string on the right. | ($a cmp $b) returns -1. |
The output result of the execution of the above program is:
$a = abc, $b = xyzabc lt $b returns true$a gt $b returns false$a le $b returns true$a ge $b returns false$a ne $b returns true$a cmp $b returns -1
In the table example, we set the variables $a to 10 and $b to 20.
operator | describe | Example |
---|---|---|
= | Simple assignment operator, assigns the value of the right operand to the left operand | $c = $a + $b will assign the value of $a + $b to $c |
+= | The addition and assignment operator assigns the result of adding the right operand to the left operand to the left operand. | $c += $a is equal to $c = $c + $a |
-= | The subtraction AND assignment operator assigns the result of subtracting the right operand from the left operand to the left operand. | $c -= $a is equal to $c = $c - $a |
*= | The multiplication and assignment operator assigns the result of multiplying the right operand by the left operand to the left operand. | $c *= $a is equal to $c = $c * $a |
/= | The division and assignment operator assigns the result of dividing the left operand by the right operand to the left operand. | $c /= $a is equal to $c = $c / $a |
%= | Modulo and assignment operator, finds the modulus of two operands and assigns it to the left operand | $c %= $a is equivalent to $c = $c % a |
**= | The exponentiation and assignment operator finds the power of the two operands and assigns them to the left operand. | $c **= $a is equal to $c = $c ** $a |
The output result of the execution of the above program is:
$a = 10, $b = 20. After assignment, $c = 30$c = 40, operation statement $c += $a$c = 30, operation statement $c -= $a$c = 300, operation statement $c *= $a$c = 30, operation statement $c /= $a$c = 0, operation statement $c %= $a$a = 4, $c = 2$c = 16, operation statement $c **= $a
Bitwise operators operate on bits and perform operations bit by bit.
Setting $a = 60, $b = 13, now in binary format, they look like this:
$a = 0011 1100$b = 0000 1101-----------------$a&$b = 0000 1100$a|$b = 0011 1101$a^$b = 0011 0001 ~$a = 1100 0011
The bitwise operators supported by Perl are shown in the following table:
operator | describe | Example |
---|---|---|
& | The binary AND operator copies one bit to the result if both operands are present. | ($a & $b) will give you 12, which in binary is 0000 1100 |
| | The binary OR operator copies one bit to the result if present in either operand. | ($a | $b) will get 61, which is 0011 1101 in binary |
^ | The binary XOR operator copies a bit to the result if it is present in one of the operands but not both. | ($a ^ $b) will give you 49, which in binary is 0011 0001 |
~ | The binary complement operator is a unary operator that has the effect of "flipping" bits, that is, 0 becomes 1 and 1 becomes 0. | (~$a ) will give -61, which is 1100 0011 in binary, the one's complement form of a signed binary number. |
<< | Binary left shift operator. The value of the left operand is shifted left by the number of bits specified by the right operand. | $a << 2 will give you 240, which is 1111 0000 in binary |
>> | Binary right shift operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand. | $a >> 2 will get 15 which in binary is 0000 1111 |
The output result of the execution of the above program is:
$a = 60 , $b = 13$a & $b = 12$a | $b = 61$a ^ $b = 49~$a = -61$a << 2 = 240$a >> 2 = 15
Perl logical operators are shown in the following table.
In the table example, we set the variable $a to true and $b to false.
operator | describe | Example |
---|---|---|
and | Logical AND operator. If both operands are true, the condition is true. | ($a and $b) is false. |
&& | C-style logical AND operator. The condition is true if both operands are true | ($a && $b) is false. |
or | Logical OR operator. The condition is true if either of the two operands is non-zero. | ($a or $b) is true. |
|| | C-style logical OR operator. The condition is true if either of the two operands is non-zero. | ($a || $b) is true. |
not | Logical NOT operator. Used to invert the logical state of the operand. If the condition is true, the logical NOT operator will make it false. | not($a and $b) is true. |
The output result of the execution of the above program is:
$a = true , $b = false$a and $b = false$a && $b = false$a or $b = true$a || $b = truenot($a)= 1
The Perl quote operators are shown in the following table.
operator | describe | Example |
---|---|---|
q{ } | Add single quotes to string | q{abcd} results in 'abcd' |
qq{ } | Add double quotes to string | qq{abcd} results in "abcd" |
qx{ } | Add backticks to string | qx{abcd} results in `abcd` |
The output result of the execution of the above program is:
q{a = $a} = a = $aqq{a = $a} = a = 10qx{date} = Friday, June 10, 2016 16:22:33 CST
In addition to the operators we mentioned above, Perl also supports the following operators:
operator | describe | Example |
---|---|---|
. | The period (.) is used to concatenate two strings. | If $a="run", $b="oob" , the result of $a.$b is "codercto" |
x | The x operator returns the number of times a string is repeated. | ('-' x 3) The output is ---. |
.. | .. is the range operator. | (2..5) The output result is (2, 3, 4, 5) |
++ | Increment operator, integer value increases by 1 | $a =10, $a++ will output 11 |
-- | Decrement operator, decrements an integer value by 1 | $a =10, $a-- the output is 9 |
-> | Arrows are used to specify methods of a class | $obj->$a represents the $a method of object $obj. |
The output result of the execution of the above program is:
$a = run , $b = oob$a . $b = codercto"-" x 3 = ---(2..5) = 2 3 4 5$a = 10 , $b = 15$a execute $a++ = 11$b execute $b-- = 14
The following table lists the operator precedence of the Perl language:
operator | associativity |
---|---|
++, -- | none |
-, ~, ! | right to left |
** | right to left |
=~, !~ | from left to right |
*, /, %, x | from left to right |
+, -, . | from left to right |
<<, >> | from left to right |
-e, -r, | none |
<, <=, >, >=, lt, le, gt, ge | from left to right |
==, !=, <=>, eq, ne, cmp | from left to right |
& | from left to right |
|, ^ | from left to right |
&& | from left to right |
|| | from left to right |
.. | from left to right |
? and : | right to left |
=, +=, -=, *=, | right to left |
other | |
, | from left to right |
not | from left to right |
and | from left to right |
or, xor | from left to right |
The output result of the execution of the above program is:
$a = 20, $b = 10, $c = 15, $d = 5($a + $b) * $c / $d = 90(($a + $b) * $c) / $d = 90($a + $b) * ($c / $d ) = 90$a + ($b * $c )/ $d = 50