Perl Operators   «Prev  Next»
Lesson 4 Perl Math operators
ObjectiveEvaluate example expressions without running them in Perl.

Perl Math Operators

The most common operators are the simple mathematical operators. These operators work with scalar values in a numeric context.
OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
**Exponentiation
<<Bitwise shift left
>>Bitwise shift right

Of these operators, exponentiation is the highest precedence, followed by the multiplicative
  1. *
  2. /
  3. %
, then the additive (+ -), and finally the shift operators.
All of these operators evaluate their left side first, which means that if two operators of the same precedence are in the same expression, the leftmost will evaluate before the rightmost operator.

Addition and Subtraction using Perl

The addition operator returns the sum of two numbers:
$x = 5 + 7;  # 12

The subtraction operator returns the difference of two numbers:
$x = 12 - 7;  # 5

Dot Operator

Unlike many other languages, the dot operator (.) is used for string concatenation instead of the + operator. Not only is this visually distinctive, but also it tells Perl to treat the data as strings instead of numbers.
my $first = 1;
my $second = 2;
my $string = $first . $second;
my $answer = $first + $second;
print "$string - $answer";
The previous code prints 12 - 3. This is because the concatenation operator considers the 1 and 2 to be strings and concatenates (joins) them. The addition operator, +, expects numbers and adds the 1 and 2 together, giving the answer of 3. You can also "chain" together multiple concatenation operators. The following code shows one way to join two strings with a space:
my $full_name = $first_name . " "  . $last_name;

Multiplication and Division

The multiplication operator returns the product of two numbers:
$x = 7 * 3;  # 21
The division operator returns the dividend of the left-side number divided by the right-side number:
$x = 21 / 7;  # 3
The modulus operator returns the integer remainder of a division operation:
$x = 22 % 7;  # 1

The exponentiation operator returns the left-hand number raised to the power of the right-hand number:
$x = 5 ** 3;  # 125

Perl Shift Operators

A bit shift is the movement of bits left or right in a computer word, which has the effect of multiplying or dividing by a power of two.
The bitwise shift operators employ the bit shift upon the value on the left side of the expression.
Shift left (<<) has the effect of doubling the value. Shift right (>>) has the effect of dividing the value in half.
The value on the right side of the expression tells you how many times to double or halve the value on the left side of the expression.
Here are a few examples:

$x = 12 << 1; # $x is 24
$x = 24 >> 1; # $x is 12

$x =  12 << 3; # $x is 96
$x = 192 >> 3; # $x is 24

Let us look at the last two examples a little more closely.
$x = 12 << 3;

In this expression, 12 is doubled three times. 12 doubled once is 24, 12 doubled twice is 48, and 12 doubled three times is 96.
$x = 192 >> 3;
In this expression, 192 is halved 3 times. 192 halved once is 96, 192 halved twice is 48, and 192 halved three times is 24.

Evaluate Perl Expressions - Exercise

Click the Exercise link below to evaluate several expressions.
Evaluate Perl Expressions - Exercise

Advanced Perl Programming