Perl operator

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation, such as: 3 plus 2 plus 5.

The Perl language has rich operators built in, so let's look at a few commonly used:


Arithmetic operator

In the table example, we set the $a to 10 and $b to 20.

Operator Describe Instance
+ Addition $a result $b is 30
- Subtract operations $a - $b result is -10
* Multiplication $a the $b result is 200
/ Division operations $b / $a result is 2
% Residual operation, the remaining number after the division $b % $a result is 0
** Multiply the power $a 20 $b that have a result of 10

Instance

#!/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 output of the above program execution 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 $a to 10 and $b to 20.

Operator Describe Instance
== Check that the values of the two operasts are equal, and if they are equal, the condition is true, otherwise it is false. ($a is $b) is false
!= Check that the values of the two operasts are equal, and if they are not equal, the condition is true, otherwise it is false. ($a! $b) is true.
<=> Check that the values of the two operans are equal, if the number on the left is less than the number on the right returns -1, if the number on the left is greater than the number on the right returns -1, if the number on the left is greater than the number on the right returns 1. ($a -lt;$b) -1.
> Check that the value of the left operanzo is greater than the value of the right operans, or true if so, otherwise false. ($a) $b false.
< Check that the value of the left operanzo is less than the value of the right operans, and if so, the condition is true, otherwise false is returned. ($a slt; $b) returns true.
>= Check that the value of the left operanzo is greater than or equal to the value of the right operans, or if so, the condition is true, otherwise false is returned. ($a ) returns false $b.
<= Check that the value of the left operanzo is less than or equal to the value of the right operans, or if so, the condition is true, otherwise false is returned. ($a is true $b)

Instance

#!/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 output of the above program execution 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

Set the variables $a "abc" and $b "xyz" in the following table instance, and then use the comparison operator to calculate the results.

Operator Describe Instance
lt Check that the string on the left is smaller than the string on the right, or if true is returned, or false is returned. ($a lt $b) returns true.
gt Check if the string on the left is larger than the string on the right, or if true is returned, or false is returned. ($ A GT $ B) Returns false.
le Check whether the string on the left is less than or equal to the string on the right, or if true is returned, or false is returned. ($a le $b) returns true
ge Check that the string on the left is greater than or equal to the string on the right, or if true is returned, or false is returned. ($a ge $b) return false.
eq Check that the string on the left is equal to the string on the right, or if true is returned, or false is returned. ($a eq $b) return false.
ne Check that the string on the left is not equal to the string on the right, or if true is returned, or false is returned. ($a ne $b) returns true
Cmp If the string on the left is larger than the string on the right returns 1, if the string on the left returns 0, if the string on the left is less than the string on the right returns -1. ($a cmp $b) returns -1.

Instance

#!/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 output of the above program execution 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

The assignment operator

In the table example, we set the $a to 10 and $b to 20.

Operator Describe Instance
= A simple assignment operator that assigns the value of the right operans to the left-hand operans $c value $a , $b will assign $a value $b the value of the $c
+= Add and assign the operator to assign the result of the right operans plus the left operastr to the left operastr $c is equal $a the value of $c , $c , $a
-= Subtract and assign the operator, and assign the result of the left operans minus the right operastr to the left operastr $c - - $a is equal to $c - $c - $a
*= Multiply and assign the operator, multiplying the right operans by the result of the left operans to the left operators $c the value $a is equal to $c , $c is $a
/= Divide and assign the operator, divide the left operans by the results of the right operans and assign them to the left operators $c the $a is equal $c / $c / $a
%= The mod and assignment operator, the module assignment of the two operasts to the left operans $c % is $a is equal to $c $c % a
**= Multiply the power and assign the operator, and the power of the two operasts is assigned to the left operator $c is equal $a to the $c , $c is $a

Instance

#!/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 output of the above program execution 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 operation

Bit operators act on bits and perform operations bit by bit.

Set $a s 60, $b s 13, now in binary format, as follows:

$a = 0011 1100

$b = 0000 1101

-----------------

$a&$b = 0000 1100

$a|$b = 0011 1101

$a^$b = 0011 0001

~$a  = 1100 0011

The bit operators supported by Perl are shown in the following table:

Operator Describe Instance
& If both operands exist at the same time, the binary AND operator copies one bit into the result. ($a and $b) will get 12, binary to 0000 1100
| If it exists in either operand, the binary OR operator copies one bit into the result. ($a | $b) will get 61 and the binary is 0011 1101
^ If it exists in one of the operands but not both, the binary heteronormation or operator copies one bit into the result. ($a s $b) will get 49 with binary 0011 0001
~ The binary complement operator is a binary operator with a "flip" bit effect. (-$a) will get -61, binary in the form of 1100 0011, 2 complements, signed binary number.
<< The binary left-shift operator. T he value of the left operans moves the number of bits specified by the right-hand operans to the left. $a 2 will get 240 and the binary is 1111 0000
>> Binary right-shift operator. The value of the left operans moves to the right the number of digits specified by the right operans. $a 2 will get 15 and the binary is 0000 1111

Instance

#!/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 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

The logical operator

The Perl logical operator is shown in the table below.

In the table instance, we set the $a to true, $b to false.

Operator Describe Instance
and Logic and operators. If both operations are true, the condition is true. ($a and $b) is false.
&& C-style logic and operators. I f both operations are true, the condition is true ($a and samp; $b) is false.
or Logic or operators. If either of the two operations is non-zero, the condition is true. ($a or $b) is true.
|| C style logic or operator. If either of the two operations is non-zero, the condition is true. ($a || $b) is true.
not Logical non-operator. T he logical state used to reverse the operastr. If the condition is true, the logical non-operator makes it false. not ($a and $b) is true.

Instance

#!/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 output of the above program execution is:

$a = true , $b = false
$a and $b = false
$a && $b = false
$a or $b = true
$a || $b = true
not($a)= 1

Quotation marks

The Perl quotation mark operator is shown in the following table.

Operator Describe Instance
q{ } Add single quotes to the string q'abcd' results are 'abcd'
qq{ } Add double quotes to the string The result of qq(abcd) is "abcd"
qx{ } Add inverse quotation marks to the string qx'abcd's result is 'abcd'

Instance

#!/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 output of the above program execution 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 supports the following operators:

Operator Describe Instance
. Dots (.) are used to connect two strings. If $a is "run", $b is "oob", $a.$b result is "youj"
The x operator returns the number of times the string has been repeated. ('-' x 3) output is ---。
.. .. is a range operator. (2..5) Output (2, 3, 4, 5)
++ Self-adding operator, integer value increased by 1 $a output of the $a is 11
-- Self-subtract operator, the integer value is reduced by 1 $a s 10, $a -- output is 9
-> Arrow number is used to specify the method of a class $obj-gt;$a the method $obj the object $a object.

Instance

#!/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 output of the above program execution is:

$a  = run , $b = oob
$a . $b = youj
"-" 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 priorities for the Perl language:

The operator Binding
++, -- No
-, ~, ! Right to left
** Right to left
=~, !~ Left to right
*, /, %, x Left to right
+, -, . Left to right
<<, >> Left to right
-e, -r, No
<, <=, >, >=, lt, le, gt, ge Left to right
==, !=, <=>, eq, ne, cmp Left to right
& Left to right
|, ^ Left to right
&& Left to right
|| Left to right
.. Left to right
? and : Right to left
=, +=, -=, *=, Right to left
Other
, Left to right
not Left to right
and Left to right
or, xor Left to right

Instance

#!/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 of the above program execution 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