Perl Operators

Summary: in this tutorial, you’ll learn about Perl operators including numeric operators, string operators, and logical operators.

Perl OperatorsNumeric operators

Perl provides numeric operators to help you operate on numbers including arithmetic, Boolean and bitwise operations. Let’s examine the different kinds of operators in more detail.

Arithmetic operators

Perl arithmetic operators deal with basic math such as adding, subtracting, multiplying, diving, etc. To add (+ ) or subtract (-) numbers, you would do something as follows:

#!/usr/bin/perl use warnings; use strict; print 10 + 20, "\n"; # 20 print 20 - 10, "\n"; # 10
Code language: Perl (perl)

To multiply or divide numbers, you use divide (/) and multiply (*) operators as follows:

#!/usr/bin/perl use warnings; use strict; print 10 * 20, "\n"; # 200 print 20 / 10, "\n"; # 2
Code language: PHP (php)

When you combine adding, subtracting, multiplying, and dividing operators together, Perl will perform the calculation in an order, which is known as operator precedence.

The multiply and divide operators have higher precedence than add and subtract operators, therefore, Perl performs multiplying and dividing before adding and subtracting. See the following example:

print 10 + 20/2 - 5 * 2 , "\n"; # 10
Code language: Perl (perl)

Perl performs 20/2 and 5*2 first, therefore you will get 10 + 10 – 10 = 10.

You can use brackets () to force Perl to perform calculation based on precedence you want as shown in the following example:

print (((10 + 20)/2 - 5) * 2); # 20;
Code language: Perl (perl)

To raise one number to the power of another number, you use exponentiation operator (**) e.g., 2**3 = 2 * 2 * 2. The following example demonstrates the exponentiation operators:

#!/usr/bin/perl use warnings; use strict; print 2**3, "\n"; # = 2 * 2 * 2 = 8. print 3**4, "\n"; # = 3 * 3 * 3 * 3 = 81.
Code language: Perl (perl)

To get the remainder of the division of one number by another, you use modulo operator (%).

It is handy to use the modulo operator (%) to check if a number is odd or even by dividing it by 2 to get the remainder. If the remainder is zero, the number is even, otherwise, the number is odd. See the following example:

#!/usr/bin/perl use warnings; use strict; print 4 % 2, "\n"; # 0 even print 5 % 2, "\n"; # 1 odd
Code language: Perl (perl)

Bitwise Operators

Bitwise operators allow you to operate on numbers one bit at a time. Think a number as a series of bits e.g., 125 can be represented in binary form as 1111101. Perl provides all basic bitwise operators including and (&), or (|), exclusive or (^) , not (~) operators, shift right (>>) and shift left (<<) operators.

The bitwise operators perform from right to left. In other words, bitwise operators perform from rightmost bit to the left most bit.

The following example demonstrates all bitwise operators:

#!/usr/bin/perl use warnings; use strict; my $a = 0b0101; # 5 my $b = 0b0011; # 3 my $c = $a & $b; # 0001 or 1 print $c, "\n"; $c = $a | $b; # 0111 or 7 print $c, "\n"; $c = $a ^ $b; # 0110 or 6 print $c, "\n"; $c = ~$a; # 11111111111111111111111111111010 (64bits computer) or 4294967290 print $c, "\n"; $c = $a >> 1; # 0101 shift right 1 bit, 010 or 2 print $c, "\n"; $c = $a << 1; # 0101 shift left 1 bit, 1010 or 10 print $c, "\n";
Code language: Perl (perl)

If you are not familiar with bitwise operations, we are highly recommended you check it out bitwise operations on Wikipedia.

Comparison operators for numbers

Perl provides all comparison operators for numbers as listed in the following table:

EqualityOperators
Equal==
Not Equal!=
Comparison<=>
Less than<
Greater than>
Less than or equal<=
Greater than or equal>=

All the operators in the table above are obvious except the number comparison operator <=> which is also known as spaceship operator.

The number comparison operator is often used in sorting numbers. See the code below:

$a <=> $b
Code language: Perl (perl)

The number operator returns:

  • 1 if $a is greater than $b
  • 0 if $a and $b are equal
  • -1 if $a is lower than $b

Take a look at the following example:

#!/usr/bin/perl use warnings; use strict; my $a = 10; my $b = 20; print $a <=> $b, "\n"; $b = 10; print $a <=> $b, "\n"; $b = 5; print $a <=> $b, "\n";
Code language: Perl (perl)

String operators

String comparison operators

Perl provides the corresponding comparison operators for strings. Let’s take a look a the table below:

EqualityOperators
Equaleq
Not Equalne
Comparisoncmp
Less thanlt
Greater thangt
Less than or equalle
Greater than or equalge

String concatenation operators

Perl provides the concatenation ( .) and repetition ( x) operators that allow you to manipulate strings. Let’s take a look at the concatenation operator (.) first:

print "This is" . " concatenation operator" . "\n";
Code language: Perl (perl)

The concatenation operator (.) combines two strings together.

A string can be repeated with the repetition ( x) operator:

print "a message " x 4, "\n";
Code language: Perl (perl)

The chomp() operator

The chomp() operator (or function) removes the last character in a string and returns a number of characters that was removed. The chomp() operator is very useful when dealing with user’s input, because it helps you remove the new line character \n from the string that user entered.

#!/usr/bin/perl use warnings; use strict; my $s; chomp($s = <STDIN>); print $s;
Code language: Perl (perl)

The <STDIN> is used to get input from users.

Logical operators

Logical operators are often used in control statements such as if, whilegiven, etc., to control the flow of the program. The following are logical operators in Perl:

  • $a && $b performs logical AND of two variables or expressions. The logical && operator checks if both variables or expressions are true.
  • $a || $b performs logical OR of two variables or expressions. The logical || operator checks either a variable or expression is true.
  • !$a performs logical NOT of the variable or expression. The logical ! operator inverts the value of the followed variable or expression. In the other words, it converts true to false or false to true.

You will learn how to use logical operators in the conditional statements such as ifwhile and given.

In this tutorial, you’ve learned some basic Perl operators. These operators are very important so make sure that you get familiar with them.

Was this tutorial helpful ?