Perl Tutorial on Perl Operators

what is an operator?

simple answer can be given using the expression 4 + 5 is equal to 9. here 4 and 5 are called operands and + is called operator. perl language supports many operator types, but following is a list of important and most frequently used operators −

  • arithmetic operators
  • equality operators
  • logical operators
  • assignment operators
  • bitwise operators
  • logical operators
  • quote-like operators
  • miscellaneous operators

lets have a look at all the operators one by one.

perl arithmetic operators

assume variable $a holds 10 and variable $b holds 20, then following are the perl arithmatic operators −

show example

sr.no. operator & description
1

+ ( addition )

adds values on either side of the operator

example − $a + $b will give 30

2

- (subtraction)

subtracts right hand operand from left hand operand

example − $a - $b will give -10

3

* (multiplication)

multiplies values on either side of the operator

example − $a * $b will give 200

4

/ (division)

divides left hand operand by right hand operand

example − $b / $a will give 2

5

% (modulus)

divides left hand operand by right hand operand and returns remainder

example − $b % $a will give 0

6

** (exponent)

performs exponential (power) calculation on operators

example − $a**$b will give 10 to the power 20

perl equality operators

these are also called relational operators. assume variable $a holds 10 and variable $b holds 20 then, lets check the following numeric equality operators −

show example

sr.no. operator & description
1

== (equal to)

checks if the value of two operands are equal or not, if yes then condition becomes true.

example − ($a == $b) is not true.

2

!= (not equal to)

checks if the value of two operands are equal or not, if values are not equal then condition becomes true.

example − ($a != $b) is true.

3

<=>

checks if the value of two operands are equal or not, and returns -1, 0, or 1 depending on whether the left argument is numerically less than, equal to, or greater than the right argument.

example − ($a <=> $b) returns -1.

4

> (greater than)

checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.

example − ($a > $b) is not true.

5

< (less than)

checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.

example − ($a < $b) is true.

6

>= (greater than or equal to)

checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.

example − ($a >= $b) is not true.

7

<= (less than or equal to)

checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.

example − ($a <= $b) is true.

below is a list of equity operators. assume variable $a holds "abc" and variable $b holds "xyz" then, lets check the following string equality operators −

show example

sr.no. operator & description
1

lt

returns true if the left argument is stringwise less than the right argument.

example − ($a lt $b) is true.

2

gt

returns true if the left argument is stringwise greater than the right argument.

example − ($a gt $b) is false.

3

le

returns true if the left argument is stringwise less than or equal to the right argument.

example − ($a le $b) is true.

4

ge

returns true if the left argument is stringwise greater than or equal to the right argument.

example − ($a ge $b) is false.

5

eq

returns true if the left argument is stringwise equal to the right argument.

example − ($a eq $b) is false.

6

ne

returns true if the left argument is stringwise not equal to the right argument.

example − ($a ne $b) is true.

7

cmp

returns -1, 0, or 1 depending on whether the left argument is stringwise less than, equal to, or greater than the right argument.

example − ($a cmp $b) is -1.

perl assignment operators

assume variable $a holds 10 and variable $b holds 20, then below are the assignment operators available in perl and their usage −

show example

sr.no. operator & description
1

=

simple assignment operator, assigns values from right side operands to left side operand

example − $c = $a + $b will assigned value of $a + $b into $c

2

+=

add and assignment operator, it adds right operand to the left operand and assign the result to left operand

example − $c += $a is equivalent to $c = $c + $a

3

-=

subtract and assignment operator, it subtracts right operand from the left operand and assign the result to left operand

example − $c -= $a is equivalent to $c = $c - $a

4

*=

multiply and assignment operator, it multiplies right operand with the left operand and assign the result to left operand

example − $c *= $a is equivalent to $c = $c * $a

5

/=

divide and assignment operator, it divides left operand with the right operand and assign the result to left operand

example − $c /= $a is equivalent to $c = $c / $a

6

%=

modulus and assignment operator, it takes modulus using two operands and assign the result to left operand

example − $c %= $a is equivalent to $c = $c % a

7

**=

exponent and assignment operator, performs exponential (power) calculation on operators and assign value to the left operand

example − $c **= $a is equivalent to $c = $c ** $a

perl bitwise operators

bitwise operator works on bits and perform bit by bit operation. assume if $a = 60; and $b = 13; now in binary format they will be as follows −

$a = 0011 1100

$b = 0000 1101

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

$a&$b = 0000 1100

$a|$b = 0011 1101

$a^$b = 0011 0001

~$a  = 1100 0011

there are following bitwise operators supported by perl language, assume if $a = 60; and $b = 13

show example

sr.no. operator & description
1

&

binary and operator copies a bit to the result if it exists in both operands.

example − ($a & $b) will give 12 which is 0000 1100

2

|

binary or operator copies a bit if it exists in eather operand.

example − ($a | $b) will give 61 which is 0011 1101

3

^

binary xor operator copies the bit if it is set in one operand but not both.

example − ($a ^ $b) will give 49 which is 0011 0001

4

~

binary ones complement operator is unary and has the efect of 'flipping' bits.

example − (~$a ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.

5

<<

binary left shift operator. the left operands value is moved left by the number of bits specified by the right operand.

example − $a << 2 will give 240 which is 1111 0000

6

>>

binary right shift operator. the left operands value is moved right by the number of bits specified by the right operand.

example − $a >> 2 will give 15 which is 0000 1111

perl logical operators

there are following logical operators supported by perl language. assume variable $a holds true and variable $b holds false then −

show example

sr.no. operator & description
1

and

called logical and operator. if both the operands are true then then condition becomes true.

example − ($a and $b) is false.

2

&&

c-style logical and operator copies a bit to the result if it exists in both operands.

example − ($a && $b) is false.

3

or

called logical or operator. if any of the two operands are non zero then then condition becomes true.

example − ($a or $b) is true.

4

||

c-style logical or operator copies a bit if it exists in eather operand.

example − ($a || $b) is true.

5

not

called logical not operator. use to reverses the logical state of its operand. if a condition is true then logical not operator will make false.

example − not($a and $b) is true.

quote-like operators

there are following quote-like operators supported by perl language. in the following table, a {} represents any pair of delimiters you choose.

show example

sr.no. operator & description
1

q{ }

encloses a string with-in single quotes

example − q{abcd} gives 'abcd'

2

qq{ }

encloses a string with-in double quotes

example − qq{abcd} gives "abcd"

3

qx{ }

encloses a string with-in invert quotes

example − qx{abcd} gives `abcd`

miscellaneous operators

there are following miscellaneous operators supported by perl language. assume variable a holds 10 and variable b holds 20 then −

show example

sr.no. operator & description
1

.

binary operator dot (.) concatenates two strings.

example − if $a = "abc", $b = "def" then $a.$b will give "abcdef"

2

x

the repetition operator x returns a string consisting of the left operand repeated the number of times specified by the right operand.

example − ('-' x 3) will give ---.

3

..

the range operator .. returns a list of values counting (up by ones) from the left value to the right value

example − (2..5) will give (2, 3, 4, 5)

4

++

auto increment operator increases integer value by one

example − $a++ will give 11

5

--

auto decrement operator decreases integer value by one

example − $a-- will give 9

6

->

the arrow operator is mostly used in dereferencing a method or variable from an object or a class name

example − $obj->$a is an example to access variable $a from object $obj.

perl operators precedence

the following table lists all operators from highest precedence to lowest.

show example

left	terms and list operators (leftward)
left	->
nonassoc	++ --
right	**
right	! ~ \ and unary + and -
left	=~ !~
left	* / % x
left	+ - .
left	<< >>
nonassoc	named unary operators
nonassoc	< > <= >= lt gt le ge
nonassoc	== != <=> eq ne cmp ~~
left	&
left	| ^
left	&&
left	|| //
nonassoc	..  ...
right	?:
right	= += -= *= etc.
left	, =>
nonassoc	list operators (rightward)
right	not
left	and
left	or xor