Perl operators
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 rich set of built-in operators. Let’s take a look at the commonly used ones:
Arithmetic operators
Comparison operators
Logical operators
Assignment operators
Bitwise operators
Quote operator
Other operators
Operator precedence
Arithmetic operators
In the table example, we set the variable $a to 10 and $b to 20.
Operator | Description | Instance |
---|---|---|
+ | The result of addition operation | $a + $b is 30 |
- | The result of subtraction operation | $a - $b is -10 |
Multiplication operation | $a * $b The result is 200 | |
Division operation | $b / $a The result is 2 | |
Remainder operation, the remainder after integer division | $b % $a The result is 0 | |
Multiple to the power | $a**$b The result is 10 20th power |
#!/usr/bin/perl
$a = 10;
$b = 20;
print "$a = $a , $b = $b\n";
$c = $a + $b;
print '$a + $b = ' . $c . "\n";
$c = $a - $b;
print '$a - $b = ' . $c . "\n";
$c = $a * $b;
print '$a * $b = ' . $c . "\n";
$c = $a / $b;
print '$a / $b = ' . $c . "\n";
$c = $a % $b;
print '$a % $b = ' . $c. "\n";
$a = 2;
$b = 4;
$c = $a ** $b;
print '$a ** $b = ' . $c . "\n";
The above program execution output result is: $a = 10 , $b = 20 $a + $b = 30 $a - $b = -10 $a * $b = 200 $a / $b = 0.5 $a % $b = 10 $a ** $b = 16
Comparison operator
In the table example, we set the variable $a to 10 and $b to 20.
Description | Instance | |
---|---|---|
Check whether the values of the two operands are equal. If they are equal, the condition is true, otherwise it is false. | ($a == $b) is false | |
Check whether the values of the two operands are equal, if not equal The condition is true, otherwise it is false. | ($a != $b) is true. | |
Check whether the values of the two operands are equal. If the number on the left is less than the number on the right, -1 is returned. If they are equal, 0 is returned. Returns 1 if the number on the left is greater than the number on the right. | ($a <=> $b) returns -1. | |
Check whether the value of the left operand is greater than the value of the right operand. If so, 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, the condition is true, otherwise it returns false. | ($a < $b) returns true. | |
Check whether the value of the left operand is greater than or equal to the value of the right operand. If so, 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, the condition is true, otherwise it returns false. . | ($a <= $b) returns true. |
Operator | Description | Instance |
---|---|---|
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 | Check whether the string on the left is greater than the string on the right, if so, return true, otherwise return false. | ($a gt $b) returns false. |
le | Check whether the string on the left is less than or equal to the string on the right, if so, return true, otherwise return false. | ($a le $b) Return true |
ge | Check whether the string on the left is greater than or equal to the string on the right, if so Returns true, otherwise returns false. | ($a ge $b) returns false. |
eq | Check whether the string on the left is equal to the string on the right, if so, return true, otherwise return false. | ($a eq $b) returns false. |
ne | Check whether the string on the left is not equal to the string on the right, if so, return true, otherwise return false. | ($a ne $b) Return true |
cmp | If the string on the left is greater than the string on the right, return 1, if they are equal, return 0, or -1 if the string on the left is less than the string on the right. | ($a cmp $b) returns -1. |
Example
#!/usr/bin/perl $a = "abc"; $b = "xyz"; print "$a = $a ,$b = $b\n"; if( $a lt $b ){ print "$a lt $b 返回 true\n"; }else{ print "$a lt $b 返回 false\n"; } if( $a gt $b ){ print "$a gt $b 返回 true\n"; }else{ print "$a gt $b 返回 false\n"; } if( $a le $b ){ print "$a le $b 返回 true\n"; }else{ print "$a le $b 返回 false\n"; } if( $a ge $b ){ print "$a ge $b 返回 true\n"; }else{ print "$a ge $b 返回 false\n"; } if( $a ne $b ){ print "$a ne $b 返回 true\n"; }else{ print "$a ne $b 返回 false\n"; } $c = $a cmp $b; print "$a cmp $b 返回 $c\n";
The execution output of the above program is:
$a = abc ,$b = xyz abc lt $b 返回 true $a gt $b 返回 false $a le $b 返回 true $a ge $b 返回 false $a ne $b 返回 true $a cmp $b 返回 -1
Assignment operator
In the table example, we set the variable $a to 10, $b is 20.
Operator | Description | Instance |
---|---|---|
= | 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 |
+= | 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 |
-= | 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 |
#*= | Multiplication and assignment operator, multiplies the right operand Assign the result of the left operand to the left operand | $c *= $a is equal to $c = $c * $a |
/= | 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, find the modulus of two operands and assign it to the left operand | $c %= $a is equal to $c = $c % a |
**= | The exponentiation and assignment operator finds the power of the two operands and assigns it to the left operand | $c **= $a is equivalent to $c = $c ** $a |
Example
#!/usr/bin/perl $a = 10; $b = 20; print "$a = $a ,$b = $b\n"; $c = $a + $b; print "赋值后 $c = $c\n"; $c += $a; print "$c = $c ,运算语句 $c += $a\n"; $c -= $a; print "$c = $c ,运算语句 $c -= $a\n"; $c *= $a; print "$c = $c ,运算语句 $c *= $a\n"; $c /= $a; print "$c = $c ,运算语句 $c /= $a\n"; $c %= $a; print "$c = $c ,运算语句 $c %= $a\n"; $c = 2; $a = 4; print "$a = $a , $c = $c\n"; $c **= $a; print "$c = $c ,运算语句 $c **= $a\n";
The above program execution output result is:
$a = 10 ,$b = 20 赋值后 $c = 30 $c = 40 ,运算语句 $c += $a $c = 30 ,运算语句 $c -= $a $c = 300 ,运算语句 $c *= $a $c = 30 ,运算语句 $c /= $a $c = 0 ,运算语句 $c %= $a $a = 4 , $c = 2 $c = 16 ,运算语句 $c **= $a
Bit operations
Bit operators act on bits and perform operations bit by bit.
Set $a = 60, $b = 13, now expressed 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 | Description | Instance |
---|---|---|
& | If both exist in The binary AND operator copies one of the two operands into the result. | ($a & $b) will get 12, binary is 0000 1100 |
| | If present in any operand, binary The OR operator copies one bit into the result. | ($a | $b) will get 61, which in binary is 0011 1101 |
^ | if present in one of the operands but not Present in both operands simultaneously, the binary XOR operator copies one bit to the result. | ($a ^ $b) will get 49, which in binary is 0011 0001 |
~ | The two's complement operator is a unary operator, Has a "flipping" bit effect. | (~$a ) will give -61 , which is 1100 0011 in binary, 2's complement, 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 get 240 , which in binary is 1111 0000 |
>> | 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, binary is 0000 1111 |
#!/usr/bin/perl
use integer;
$a = 60;
$b = 13;
print "$a = $a , $b = $b\n";
$c = $a & $b;
print "$a & $b = $c\n";
$c = $a | $b;
print "$a | $b = $c\n";
$c = $a ^ $b;
print "$a ^ $b = $c\n";
$c = ~$a;
print "~$a = $c\n";
$c = $a << 2;
print "$a << 2 = $c\n";
$c = $a >> 2;
print "$a >> 2 = $c\n";
The output result of the above program execution is :$a = 60 , $b = 13 $a & $b = 12 $a | $b = 61 $a ^ $b = 49 ~$a = -61 $a << 2 = 240 $a >> 2 = 15
Logical operatorsPerl logical operators are shown in the following table. In the table example, we set the variable $a to true and $b to false.
Description | Instance | |
---|---|---|
Logical AND operator. If both operands are true, the condition is true. | ($a and $b) is false. | |
C-style logical AND operator. The condition true | ($a && $b) is false if both operands are true. | |
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. | |
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. |
Operator | Description | Instance |
---|---|---|
q{ } | Add single quotes to the string | q{abcd} The result is 'abcd' |
qq{ } | Add double quotes to the string | qq{abcd} The result is "abcd" |
qx{ } | Add backticks to the string | qx{ abcd} The result is `abcd` |
Example
#!/usr/bin/perl $a = 10; $b = q{a = $a}; print "q{a = $a} = $b\n"; $b = qq{a = $a}; print "qq{a = $a} = $b\n"; # 使用 unix 的 date 命令执行 $t = qx{date}; print "qx{date} = $t\n";
The above program execution output result is:
q{a = $a} = a = $a qq{a = $a} = a = 10 qx{date} = 2016年 6月10日 星期五 16时22分33秒 CST
Other operators
In addition to the operators we mentioned above, Perl also supports the following operators:
Operator | Description | Example |
---|---|---|
. | The period (.) is used to connect two strings. | If $a="run", $b="oob", the result of $a.$b is "php" |
x | x The 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 The value increases by 1 | $a =10, $a++ will output 11 |
-- | Decrement operator, the integer value decreases 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. |
Example
#!/usr/bin/perl $a = "run"; $b = "oob"; print "$a = $a , $b = $b\n"; $c = $a . $b; print "$a . $b = $c\n"; $c = "-" x 3; print "\"-\" x 3 = $c\n"; @c = (2..5); print "(2..5) = @c\n"; $a = 10; $b = 15; print "$a = $a , $b = $b\n"; $a++; $c = $a ; print "$a 执行 $a++ = $c\n"; $b--; $c = $b ; print "$b 执行 $b-- = $c\n";
The execution output of the above program is:
$a = run , $b = oob $a . $b = php "-" x 3 = --- (2..5) = 2 3 4 5 $a = 10 , $b = 15 $a 执行 $a++ = 11 $b 执行 $b-- = 14
Operator priority
The following table lists the operator priority of Perl language :
Operator | Associativity |
---|---|
++, -- | None |
From right to left | |
From right To left | |
From left to right | |
From left to right | |
From left to right | |
From left to right | |
None | |
from left to right | |
from left to right | |
from left to right | |
From left to right | |
From left to right | |
From left to right | |
From left to right | |
Right to left | |
Right to left | |
From left to right | |
Left to right | |
Left to right | |
From left to right |
#!/usr/bin/perl $a = 20; $b = 10; $c = 15; $d = 5; $e; print "$a = $a, $b = $b, $c = $c ,$d = $d\n"; $e = ($a + $b) * $c / $d; print "($a + $b) * $c / $d = $e\n"; $e = (($a + $b) * $c )/ $d; print "(($a + $b) * $c) / $d = $e\n"; $e = ($a + $b) * ($c / $d); print "($a + $b) * ($c / $d ) = $e\n"; $e = $a + ($b * $c ) / $d; print "$a + ($b * $c )/ $d = $e\n";
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