Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl Operators

Perl Operators

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation, e.g.: 3+2=5.

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


Arithmetic operators

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

operator Description Example
+ Addition $a + $b gives 30
- Subtraction $a - $b gives -10
* Multiplication $a * $b gives 200
/ Division operation $b / $a gives 2
% Modulo operation, the remainder after integer division $b % $a gives 0
** Exponentiation $a**$b gives 10 to the power of 20

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";

The output 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


Comparison operators

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

operator Description Example
== Check whether the values of 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 two operands are equal; if they are not equal the condition is true, otherwise it is false. ($a != $b) is true.
<=> Check the values of two operands; if the left number is less than the right one return -1, if equal return 0, if the left number is greater than the right one return 1. ($a <=> $b) returns -1.
> Check whether the left operand is greater than the right operand; if so the condition is true, otherwise it is false. ($a > $b) returns false.
< Check whether the left operand is less than the right operand; if so the condition is true, otherwise it returns false. ($a < $b) returns true.
>= Check whether the left operand is greater than or equal to the right operand; if so the condition is true, otherwise it returns false. ($a >= $b) returns false.
<= Check whether the left operand is less than or equal to the right operand; if so the condition is true, otherwise it 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 gives true

\n

"

;

}

else

{

print

"

$

a ==

$

b gives false

\n

"

;

}

if

(

$a

!=

$b

)

{

print

"

$

a !=

$

b gives true

\n

"

;

}

else

{

print

"

$

a !=

$

b gives false

\n

"

;

}

$c

=

$a

<=>

$b

;

print

"

$

a <=>

$

b returns

$c

\n

"

;

if

(

$a

$b

)

{

print

"

$

a >

$

b gives true

\n

"

;

}

else

{

print

"

$

a >

$

b gives false

\n

"

;

}

if

(

$a

=

$b

)

{

print

"

$

a >=

$

b gives true

\n

"

;

}

else

{

print

"

$

a >=

$

b gives false

\n

"

;

}

if

(

$a

<

$b

)

{

print

"

$

a <

$

b gives true

\n

"

;

}

else

{

print

"

$

a <

$

b gives false

\n

"

;

}

if

(

$a

<=

$b

)

{

print

"

$

a <=

$

b gives true

\n

"

;

}

else

{

print

"

$

a <=

$

b gives false

\n

"

;

}

The output of the above program 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

In the following table example, the variable $a is set to "abc" and $b to "xyz", and then comparison operators are used to calculate the results.

operator Description Example
lt Check whether the left string is less than the right string; if so return true, otherwise return false. ($a lt $b) returns true.
gt Check whether the left string is greater than the right string; if so return true, otherwise return false. ($a gt $b) returns false.
le Check whether the left string is less than or equal to the right string; if so return true, otherwise return false. ($a le $b) returns true
ge Check whether the left string is greater than or equal to the right string; if so return true, otherwise return false. ($a ge $b) returns false.
eq Check whether the left string is equal to the right string; if so return true, otherwise return false. ($a eq $b) returns false.
ne Check whether the left string is not equal to the right string; if so return true, otherwise return false. ($a ne $b) returns true
cmp If the left string is greater than the right string return 1, if equal return 0, if the left string is less than the right string return -1. ($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

"

;

The 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 operators

In the table example we set the 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 assigns the value of $a + $b to $c
+= Add-and-assign 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-assign 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-assign 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-assign 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-assign operator, assigns the modulus of the two operands to the left operand $c %= $a is equivalent to $c = $c % a
**= Exponentiation and assignment operator; computes the exponentiation of 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

"

;

The output of the above program 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 operations

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

Set $a = 60 and $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) gives 12, which is 0000 1100 in binary
| If present in either operand, the binary OR operator copies a bit to the result. ($a | $b) gives 61, which is 0011 1101 in binary
^ If a bit is present in one operand but not both, the binary XOR operator copies that bit to the result. ($a ^ $b) gives 49, which is 0011 0001 in binary
~ The binary complement operator is a unary operator with a "flip" bit effect, that is, 0 becomes 1 and 1 becomes 0. (~$a ) gives -61, which is 1100 0011 in binary, the 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 gives 240, which is 1111 0000 in binary
>> Binary right shift operator. The value of the left operand is shifted right by the number of bits specified by the right operand. $a >> 2 gives 15, which is 0000 1111 in binary

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

The logical operators of Perl are shown in the following table.

In the table example we set the variable $a to true and $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 either of the two operands is nonzero, the condition is true. ($a or $b) is true.
|| C-style logical OR operator. If either of the two operands is nonzero, 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 makes 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{ } Add single quotes to a string q{abcd} results in 'abcd'
qq{ } Add double quotes to a string qq{abcd} results in "abcd"
qx{ } Add backticks to 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

"

;

Executed using the Unix date command

$t

=

qx

{date}; print "q

x

{

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

In addition to 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 "runoob"
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 refers to the $a method of the 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 executed

$

a++ =

$c

\n

"

;

$b

--;

$c

=

$b

;

print

"

$

b executed

$

b-- =

$c

\n

"

;

The output of the above program is:



$a  = run , $b = oob

$a . $b = runoob

"-" 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:

Operator Associativity
++, -- None
-, ~, ! Right to left
** Right to left
=~, !~ Left to right
*, /, %, x Left to right
+, -, . Left to right
<<, >> Left to right
-e, -r, None
<, <=, >, >=, 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
Others
, Left to right
not Left to right
and Left to right
or, xor Left 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