HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
Operator tells the compiler to perform a specific mathematical or logical operation symbols such as: 3 + 2 = 5.
Perl language built a wealth of operators, we look at several common:
Form example we set the variable $ a is 10, $ b 20.
Operators | description | Examples |
---|---|---|
+ | Adder | $ A + $ b 30 results |
- | Subtraction | $ A - $ b the result is -10 |
* | Multiplication | $ A * $ b 200 Results |
/ | Division | $ B / $ a result 2 |
% | Modulo operation, the remainder after the divisible | $ B% $ a result is 0 |
** | Exponentiation | $ A ** $ b 10 20 th power results |
#!/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 is:
$a = 10 , $b = 20 $a + $b = 30 $a - $b = -10 $a * $b = 200 $a / $b = 0.5 $a % $b = 10 $a ** $b = 16
Form example we set the variable $ a is 10, $ b 20.
Operators | description | Examples |
---|---|---|
== | Check the value of the two operands are equal, if they are equal then the condition is true, otherwise false. | ($ A == $ b) is false |
! = | Check the value of the two operands are equal, if not equal condition is true, otherwise false. | ($ A! = $ B) is true. |
<=> | Check the value of the two operands are equal, if the number is less than the number to the left of the right of return -1 return 0 if they are equal, if the number is greater than the number on the left to the right of return 1. | ($ A <=> $ b) return -1. |
> | Check the value of the left operand is greater than the value of the right operand, and if the condition is true, otherwise false. | ($ A> $ b) returns false. |
< | Check the value of the left operand is less than the value of the right operand, and if the condition is true, otherwise false. | ($ A <$ b) returns true. |
> = | Check the value of the left operand is greater than or equal to the value of the right operand, and if the condition is true, otherwise false. | ($ A> = $ b) returns false. |
<= | Check the value of the left operand is less than or equal to the value of the right operand, and if the condition is true, otherwise false. . | ($ A <= $ b) returns true. |
#!/usr/bin/perl $a = 10; $b = 20; print "\$a = $a , \$b = $b\n"; if( $a == $b ){ print "$a == \$b 结果 true\n"; }else{ print "\$a == \$b 结果 false\n"; } if( $a != $b ){ print "\$a != \$b 结果 true\n"; }else{ print "\$a != \$b 结果 false\n"; } $c = $a <=> $b; print "\$a <=> \$b 返回 $c\n"; if( $a > $b ){ print "\$a > \$b 结果 true\n"; }else{ print "\$a > \$b 结果 false\n"; } if( $a >= $b ){ print "\$a >= \$b 结果 true\n"; }else{ print "\$a >= \$b 结果 false\n"; } if( $a < $b ){ print "\$a < \$b 结果 true\n"; }else{ print "\$a < \$b 结果 false\n"; } if( $a <= $b ){ print "\$a <= \$b 结果 true\n"; }else{ print "\$a <= \$b 结果 false\n"; }
The above program execution output is:
$a = 10 , $b = 20 $a == $b 结果 false $a != $b 结果 true $a <=> $b 返回 -1 $a > $b 结果 false $a >= $b 结果 false $a < $b 结果 true $a <= $b 结果 true
The following table instance variable $ a is set to "abc", $ b to "xyz", then use a comparison operator to calculate the results.
Operators | description | Examples |
---|---|---|
lt | Check the string on the left is less than the string on the right to return if it is true, otherwise false. | ($ A lt $ b) returns true. |
gt | Check whether the string on the left is greater than the string on the right to return if it is true, otherwise false. | ($ A gt $ b) returns false. |
le | Check the string on the left is less than or equal to the string on the right, if it returns true, otherwise returns false. | ($ A le $ b) Returns true |
ge | Check whether the string on the left is greater than or equal to the string on the right, if it returns true, otherwise returns false. | ($ A ge $ b) returns false. |
eq | Check the string is equal to the string on the left to the right, if it is to return true, otherwise returns false. | ($ A eq $ b) returns false. |
ne | Check the string on the left is not equal to the string on the right to return if it is true, otherwise false. | ($ A ne $ b) Returns true |
cmp | If the string is greater than the right of the left string returns 1 if they are equal returns 0 if the string is the string to the right of the left is less than -1. | ($ A cmp $ b) return -1. |
#!/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 above program execution output 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
Form example we set the variable $ a is 10, $ b 20.
Operators | description | Examples |
---|---|---|
= | Simple assignment operator, the value on the right operand to the left operand | $ C = $ a + $ b $ a + $ b will assign the value of $ c |
+ = | Canadian and assignment operator, the result of adding the left operand to the right operand assigned to the left operand | $ C + = $ a is equal to $ c = $ c + $ a |
- = | Save and assignment operators, the left operand to the right operand is subtracted assign the result to left operand | $ C - = $ a is equal to $ c = $ c - $ a |
* = | Multiply and assignment operator, the right of the left operand is multiplied by the operand assign the result to left operand | $ C * = $ a is equal to $ c = $ c * $ a |
/ = | In addition and assignment operators, the left operand divided by the right of the operand assign the result to left operand | $ C / = $ a is equal to $ c = $ c / $ a |
% = | Modulo and assignment operator, the sum of two operands mold assigned to the left operand | $ C% = $ a is equal to $ c = $ c% a |
** = | Exponentiation and assignment operator, the sum of two operands multiplied by the power assigned to the left operand | $ C ** = $ a is equal to $ c = $ c ** $ a |
#!/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 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
Bitwise operators acting on the bit, and bit by bit operation.
Setting $ a = 60, $ b = 13, now represented in binary format, they are as follows:
$a = 0011 1100 $b = 0000 1101 ----------------- $a&$b = 0000 1100 $a|$b = 0011 1101 $a^$b = 0011 0001 ~$a = 1100 0011
Bit computing support Perl symbols as follows:
Operators | description | Examples |
---|---|---|
& | If both exist in two operands, the binary AND operator to copy a result. | ($ A & $ b) would be 12, to binary 0000 1100 |
| | If present in either operand, the binary OR operator to copy a result. | ($ A | $ b) would be 61, the binary is 00111101 |
^ | If present in one of the operand, but not simultaneously exist in two operands, binary XOR operator a copy to the result. | ($ A ^ $ b) would be 49, the binary is 00110001 |
~ | Twos complement operator is a unary operator, a "flip" position effect. | (~ $ A) would be -61, binary 1100 0011 complement form of binary number with sign. |
<< | Binary left shift operator. The value of the left operand to move left and right operand the specified number of digits. | $ A << 2 will be 240, the binary is 11110000 |
>> | Binary right shift operator. The value of the left operand move right operand the specified number of bits to the right. | $ A >> 2 will be 15, the 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 above program execution output 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 in the following table.
Form example we set the variable $ a is true, $ b is false.
Operators | description | Examples |
---|---|---|
and | Logical AND operator operator. If both operands are true, the condition is true. | ($ A and $ b) is false. |
&& | C-style logical AND operator operator. If both operands are true, the condition is true | ($ A && $ b) is false. |
or | Logical OR operator. If both operands have any non-zero, the condition is true. | ($ A or $ b) is true. |
|| | C-style Logical OR operator. If both operands have any non-zero, the condition is true. | ($ A || $ b) is true. |
not | Logical NOT operator. It inverts the logic state of the operand. If the condition is true, the logical not operator will make it to false. | not ($ a and $ b) is true. |
#!/usr/bin/perl $a = true; $b = false; print "\$a = $a , \$b = $b\n"; $c = ($a and $b); print "\$a and \$b = $c\n"; $c = ($a && $b); print "\$a && \$b = $c\n"; $c = ($a or $b); print "\$a or \$b = $c\n"; $c = ($a || $b); print "\$a || \$b = $c\n"; $a = 0; $c = not($a); print "not(\$a)= $c\n";
The above program execution output is:
$a = true , $b = false $a and $b = false $a && $b = false $a or $b = true $a || $b = true not($a)= 1
Perl quotes operators in the following table.
Operators | description | Examples |
---|---|---|
q {} | The string with a single quote | q {abcd} results for 'abcd' |
qq {} | A string of double quotes | qq {abcd} results for "abcd" |
qx {} | The string with backquotes | qx {abcd} the result is `abcd` |
#!/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 is:
q{a = $a} = a = $a qq{a = $a} = a = 10 qx{date} = 2016年 6月10日 星期五 16时22分33秒 CST
In addition to the above we mentioned operators outside, Perl also supports the following operators:
Operators | description | Examples |
---|---|---|
. | Dot (.) Is used to connect two strings. | If $ a = "run", $ b = "oob", $ a. $ B results for "w3resource" |
x | x operator returns a string of repetitions. | ( '-' X 3) output is ---. |
/en | /en In a range of operators. | (2/en5) The output is (2, 3, 4, 5) |
++ | Increment operator, the integer value is incremented by 1 | $ A = 10, $ a ++ will output 11 |
- | Decrement operators, integer value reduced by 1 | $ A = 10, $ a-- output 9 |
-> | Specify a class method for Arrow | $ Obj -> $ a representation of the object $ obj $ a method. |
#!/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/en5); print "(2/en5) = @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 above program execution output is:
$a = run , $b = oob $a . $b = w3resource "-" x 3 = --- (2/en5) = 2 3 4 5 $a = 10 , $b = 15 $a 执行 $a++ = 11 $b 执行 $b-- = 14
The following table lists the Perl language operator precedence:
Operators operator | Binding |
---|---|
++ - | no |
-, ~,! | 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, | no |
<, <=,>,> =, 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 |
/en | 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 |
#!/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 above program execution output 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