Perl Operators

Operators are symbols that tell the compiler to perform specific mathematical or logical operations, such as: 3+2=5.

The Perl language has a rich set of built-in operators. Let's look at the commonly used ones:


Arithmetic Operators

In the following table examples, we set variable $a to 10 and $b to 20.

Operator Description Example
+ Addition operation $a + $b result is 30
- Subtraction operation $a - $b result is -10
* Multiplication operation $a * $b result is 200
/ Division operation $b / $a result is 2
% Modulus operation, remainder after division $b % $a result is 0
** Exponentiation$a**$b result is 10 to the 20th power

Example

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

Executing the above program produces the following output:

$a = 10 , $b = 20
$a + $b = 30
$a - $b = -10
$a * $b = 200
$a / $b = 0.5
$a % $b = 10
$a ** $b = 16

Comparison Operators

In the following table examples, we set variable $a to 10 and $b to 20.

Operator Description Example
== Checks whether the values of two operands are equal. If equal, the condition is true, otherwise false. ($a == $b) is false
!= Checks whether the values of two operands are equal. If not equal, the condition is true, otherwise false. ($a != $b) is true.
<=> Checks whether the values of two operands are equal. Returns -1 if the left number is less than the right number, 0 if equal, and 1 if the left number is greater than the right number. ($a <=> $b) returns -1.
> Checks whether the value of the left operand is greater than the value of the right operand. If so, the condition is true, otherwise 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 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, the condition is true, otherwise 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 returns false. ($a <= $b) returns true.

Example

#!/usr/bin/perl $a = 10; $b = 20; print "\$a = $a , \$b = $b\n"; if( $a == $b ){ print "$a == \$b result true\n"; }else{ print "\$a == \$b result false\n"; } if( $a != $b ){ print "\$a != \$b result true\n"; }else{ print "\$a != \$b result false\n"; } $c = $a <=> $b; print "\$a <=> \$b returns$c\n"; if( $a > $b ){ print "\$a > \$b result true\n"; }else{ print "\$a > \$b result false\n"; } if( $a >= $b ){ print "\$a >= \$b result true\n"; }else{ print "\$a >= \$b result false\n"; } if( $a < $b ){ print "\$a < \$b result true\n"; }else{ print "\$a < \$b result false\n"; } if( $a <= $b ){ print "\$a <= \$b result true\n"; }else{ print "\$a <= \$b result false\n"; }

Executing the above program produces the following output:

$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

In the following table examples, we set variable $a to "abc" and $b to "xyz", then use comparison operators to calculate the results.

Operator Description Example
lt Checks whether the string on the left is less than the string on the right. If so, returns true, otherwise 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, returns true, otherwise 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, returns true, otherwise 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, returns true, otherwise 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, returns true, otherwise 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, returns true, otherwise returns false. ($a ne $b) returns true
cmp Returns 1 if the left string is greater than the right string, returns 0 if equal, and returns -1 if the left string is less than the right string. ($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 returns true\n"; }else{ print "\$a lt \$b returns false\n"; } if( $a gt $b ){ print "\$a gt \$b returns true\n"; }else{ print "\$a gt \$b returns false\n"; } if( $a le $b ){ print "\$a le \$b returns true\n"; }else{ print "\$a le \$b returns false\n"; } if( $a ge $b ){ print "\$a ge \$b returns true\n"; }else{ print "\$a ge \$b returns false\n"; } if( $a ne $b ){ print "\$a ne \$b returns true\n"; }else{ print "\$a ne \$b returns false\n"; } $c = $a cmp $b; print "\$a cmp \$b returns$c\n";

Executing the above program produces the following output:

$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 Operators

In the following table examples, we set variable $a to 10 and $b to 20.

Operator Description 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
+= Add and assignment operator, assigns the result of adding the right operand to the left operand to the left operand $c += $a is equivalent to $c = $c + $a
-= Subtract and assignment operator, assigns the result of subtracting the right operand from the left operand to the left operand $c -= $a is equivalent to $c = $c - $a
*= Multiply and assignment operator, assigns the result of multiplying the right operand by the left operand to the left operand $c *= $a is equivalent to $c = $c * $a
/= Divide and assignment operator, assigns the result of dividing the left operand by the right operand to the left operand $c /= $a is equivalent to $c = $c / $a
%= Modulus and assignment operator, takes the modulus of the two operands and assigns it to the left operand $c %= $a is equivalent to $c = $c % a
**= Exponentiation and assignment operator, calculates 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 "After assignment\$c = $c\n"; $c += $a; print "\$c = $c, operation statement\$c += \$a\n"; $c -= $a; print "\$c = $c, operation statement\$c -= \$a\n"; $c *= $a; print "\$c = $c, operation statement\$c *= \$a\n"; $c /= $a; print "\$c = $c , operation statement \$c/= \$a\n"; $c %= $a; print "\$c = $c, operation statement \$c %= \$a\n"; $c = 2; $a = 4; print "\$a = $a , \$c = $c\n"; $c **= $a; print "\$c = $c, operation statement \$c **= \$a\n";

Executing the above program produces the following output:

$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 Operations

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

Set $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

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

Operator Description Example
& If present in both operands, the binary AND operator copies a bit to the result. ($a & $b) will give 12, which is 0000 1100 in binary
| If present in either operand, the binary OR operator copies a bit to the result. ($a | $b) will give 61, binary 0011 1101
^ Binary XOR operator copies a bit to the result if it exists in either operand but not in both. ($a ^ $b) will give 49, binary 0011 0001
~ The binary ones complement operator is a unary operator, having the effect of "flipping" bits, that is, 0 becomes 1, 1 becomes 0. (~$a ) will give -61, binary 1100 0011, the ones complement form of a signed binary number.
<< Binary left shift operator. The left operand's value is shifted left by the number of bits specified by the right operand. $a << 2 will give 240, binary 1111 0000
>> Binary right shift operator. The left operand's value is shifted right by the number of bits specified by the right operand. $a >> 2 will give 15, binary 0000 1111

Example

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

$a = 60 , $b = 13
$a & $b = 12
$a | $b = 61
$a ^ $b = 49
~$a = -61
$a << 2 = 240
$a >> 2 = 15

Logical Operators

Perl logical operators are shown in the following table.

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

Operator Description Example
and Logical AND operator. If both operands are true, the condition is true. ($a and $b) is false.
&& C-style logical AND operator. If both operands are true, the condition is true. ($a && $b) is false.
or Logical OR operator. If any one of the operands is non-zero, the condition is true. ($a or $b) is true.
|| C-style logical OR operator. If any one of the operands is non-zero, the condition is true. ($a || $b) is true.
not Logical NOT operator. Used to reverse 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.

Example

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

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

Quote Operations

Perl quote operators are shown in the following table.

Operator Description Example
q{ } Adds single quotes around a string q{abcd} results in 'abcd'
qq{ } Adds double quotes around a string qq{abcd} results in "abcd"
qx{ } Adds backticks around a string qx{abcd} results in `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"; # Execute using the unix date command $t = qx{date}; print "qx{date} = $t\n";

The output of the above program is:

q{a = $a} = a = $a
qq{a = $a} = a = 10
qx{date} = 2016年 6月10日 星期五 16时22分33秒 CST

Other Operators

Besides the operators mentioned above, Perl also supports the following operators:

Operator Description Example
. The dot (.) is used to concatenate two strings. If $a="run", $b="oob", then $a.$b results in "example"
x The x operator returns the string repeated a specified number of times. ('-' x 3) outputs ---.
.. .. is the range operator. (2..5) outputs (2, 3, 4, 5)
++ Increment operator, increases the integer value by 1 $a =10, $a++ outputs 11
-- Decrement operator, decreases the integer value by 1 $a =10, $a-- outputs 9
-> The arrow is used to specify a method 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 executes\$a++ = $c\n"; $b--; $c = $b ; print "\$b executes\$b-- = $c\n";

The output of the above program is:

$a  = run , $b = oob
$a . $b = example
"-" x 3 = ---
(2..5) = 2 3 4 5
$a  = 10 , $b = 15
$a 执行 $a++ = 11
$b 执行 $b-- = 14

Operator Precedence

The following table lists the operator precedence of the Perl language:

OperatorAssociativity
++, --None
-, ~, !Right to left
**Right to left
=~, !~Left to right
*, /, %, xLeft to right
+, -, .Left to right
<<, >>Left to right
-e, -r,None
<, <=, >, >=, lt, le, gt, geLeft to right
==, !=, <=>, eq, ne, cmpLeft 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
notLeft to right
andLeft to right
or, xorLeft to right

Example

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