Numeric operators

In the Perl language there are available the following numeric operators:

  • arithmetic operators

  • numeric relational operators

  • numeric logical operators

  • numeric bitwise operators

  • other numeric operators

  • numeric assignment operators

 

In the table below you see the Perl operators used in arithmetic operations.

Op

Name

Definition

+

addition

It's a binary operator that returns the sum of two operands. Example:

 $v1 = 12;
 $v2 = 5;
 $v = $v1 + $v2;
 print "$v (expected 17)\n";

-

subtraction

It's a binary operator that returns the difference of two operands. Example:

 $v1 = 12;
 $v2 = 5;
 $v = $v1-$v2;
 print "$v (expected 7) \n";

-

negation

It's a unary operator that performs arithmetic negation. Example:

 $v1 = 12;
 $v2 = -$v1;
 print "$v2 (expected -12) \n";

*

multi-plication

It's a binary operator that multiplies two operands. Example:

 $v1 = 12;
 $v2 = 5;
 $v = $v1 * $v2;
 print "$v ( expected 60 ) \n";

/

division

It's a binary operator that divides left value by right value. Example:

 $v1 = 12;
 $v2 = 5;
 $v = $v1 / $v2;
 print "$v ( expected 2.4 ) \n";

**

expo-nentiation

It's a binary operator that raises the left value to the power of the right value. Example:

 $v1 = 12;
 $v2 = 2;
 $v = $v1 ** $v2;
 print "$v ( expected 144 ) \n";

%

modulus

It's a binary operator that returns the remainder of dividing left value by right value. Example:

 $v1 = 12;
 $v2 = 5;
 $v = $v1 % $v2;
 print "$v (expected 2) \n";

++

auto-increment

If you place this unary operator before/after a variable, the variable will be incremented before/after returning the value. Example:

 $v1 = 10;
 $v2 = ++$v1;
 print "$v1 $v2 (expected 11 11) \n";
 $v1 = 10;
 $v2 = $v1++;
 print "$v1 $v2 (expected 11 10) \n";

--

auto-decrement

If you place this unary operator before/after a variable, the variable will be decremented before/after returning the value. Example:

 $v1 = 10;
 $v2 = --$v1;
 print "$v1 $v2 (expected 9 9) \n";
 $v1 = 10;
 $v2 = $v1--;
 print "$v1 $v2 (expected 9 10) \n";

 

The numeric relational Perl operators compare two numbers and determine the validity of a relationship.

Op

Name

Definition

<

less than

The "less than" operator indicates if the value of the left operand is less than the value of the right one. Example:

 ($v1, $v2) = (5, 7);
 if($v1 < $v2){
 print "OK (expected) \n";
 }

<=

less than

or equal to

The "less than or equal to" operator indicates if the value of the left operand is less than or equal to the value of the right one. Example:

 ($v1, $v2) = (5, 5);
 if($v1 <= $v2){
 print "OK (expected) \n";
 }

>

greater than

The "greater than" operator indicates if the value of the left operand is greater than the value of the right one. Example:

 ($v1, $v2) = (5, 7);
 if($v2 > $v1){
 print "OK (expected) \n";
 }

>=

greater than or equal to

The "greater than or equal to" operator indicates if the value of the left operand is greater than or equal to the value of the right one. Example:

 ($v1, $v2) = (5, 5);
 if($v2 >= $v1){
 print "OK (expected) \n";
 }

==

equal

The "equal" operator returns true if the left operand is equal to the right one. Example:

 ($v1, $v2) = (5, 5);
 if($v1 == $v2){
 print "OK (expected) \n";
 }

!=

not equal to

The "not equal to" operator returns true if the left operand is not equal to the right one. Example:

 ($v1, $v2) = (5, 7);
 if($v1 != $v2){
 print "OK (expected) \n";
 }

<=>

numeric comparison

This binary operator returns -1, 0, or 1 if the left operand is less than, equal to or greater than the right one. Example:

 ($v1, $v2) = (5, 7);
 $v = $v1 <=> $v2;
 print "$v\n"; # displays -1;

 

The numeric logical Perl operators are generally derived from boolean algebra and they are mainly used to control program flow, finding them as part of an if, a while or some other control statement. See in the table below the logical numerical Perl operators.

Op

Name

Definition

!

negation

This unary operator evaluates an operand and return true if the operand has the false value (0) and false otherwise.
Example:

 $v1 = !25;
 $v2 = !0;
 print "v1=$v1, v2=$v2 \n";
 # displays v1=,v2=1

not

not

It has the same meaning as the "!" operator, described above.

 

and,

&&

and

The "and" operator returns the logical conjunction of two operands. Example:

 ($v1, $v2) = (5, 7);
 if($v1 == 5 && $v2 == 7) {
 print "OK (expected) \n";
 }

or, ||

or

The "or" operator returns the logical disjunction of two operands. Example:

 ($v1, $v2) = (5, 7);
 if($v1 == 5 || $v2 == 0) {
 print "OK (expected) \n";
 }

xor

exclusive or

The "exclusive or" operator returns the logical exclusive-or of two operands (the result is true if either but not both of the operands is true). Example:

 ($v1, $v2) = (5, 7);
 print ($v1==5 xor $v2==3); 
 # displays 1
 print ($v1==5 xor $v2==7); 
 # displays

?

condi-tional operator

This ternary operator is like the symbolic if ... then ... else clause from the C language. It returns the second operand if the leftmost operand is true and the third operand otherwise.
Example:

 $v = (2 == 2) ? "Equal\n" : "Not equal \n";
 print $v; # displays Equal

 

Numeric bitwise Perl operators are similar to the logical operators, but they work on the binary representation of data. They are used to change individual bits in an operand. Please note that both operands associated with bitwise operators are integers.

Op

Name

Definition

<<

shift left

The "shift left" << operator is a binary operator that shifts the bits to the left. Its first operand specifies the integer value to be shifted meanwhile the second one specifies the number of position that the bits in the value will be shifted. The rightmost bits of the integer value will be assigned with 0 and the leftmost bits will be discarded.

Example:

 $v = 25 << 3;
 print "$v (expected 200)\n";
 

>>

shift right

The "shift right" >> operator is a binary operator that shifts the bits to the right. Its first operand specifies the integer value to be shifted meanwhile the second one specifies the number of position that the bits in the value will be shifted. The leftmost bits of the integer value will be assigned with 0 and the rightmost bits will be discarded.

Example:

 $v = 32 >> 3;
 print "$v (expected 4)\n";
 

&

and

The "and" operator sets a bit to 1 if both of the corresponding bits in its operands are 1, and 0 otherwise.

Example:

 $v = 32 & 16;
 print "$v (expected 0)\n";
 

|

or

The "or" operator sets a bit to 0 if both of the corresponding bits in its operands are 0, and 1 otherwise.

Example:

 $v = 32 | 16;
 print "$v (expected 48)\n";
 

^

exclusive or

The "exclusive or" operator sets a bit to 1 if the corresponding bits in its operands are different, and 0 otherwise.

Example:

 $v = 3 ^ 9;
 print "$v (expected 10)\n";
 

~

not

The unary "not" operator inverts each bit in the operand, changing all the ones to zeros and zeros to ones.

Example:

 $v = ~1024;
 

 

See in the table below other numeric Perl operators:

Op

Name

Definition

,

comma

In scalar context this binary operator evaluates its left argument, discards this value, then evaluates its right argument and returns that value. In a list context, it's just a separator and inserts both its arguments into the list.

Example (in scalar context):

 $v1 = 2; $v2 = 4; $v3 = $v1 == $v2;
 $v = ($v3, 5 == 5);
 print(" $v3( expected )\n");
 print(" $v( expected 1)\n");
 

=>

comma

It has the same function like the comma operator described above.

 

..

Range operator

In scalar context, this operator returns false as long as its left operand is false. When the left operand becomes true, the range operator returns true until the right operator remains true, after which it becomes false again. In a list context, this operator will return an array with contiguous sequences of items, beginning with the left operand value and ending with the right operand value (the items can be characters or numbers).

Example (in a list context):

 print ('a'..'zz'); 
 print ('1'..'10');
 

 

Numeric assignment Perl operators perform some type of numeric operation and then assign the value to the existing variable.

Op

Name

Definition

=

assignment

This is the ordinary assignment operator. In a scalar context, it assigns the right operand's value to the left operand. In a list context, it assigns multiple values to the left array operand if the right operand is a list.

Example:

 $v = 15; 
 print $v, "\n"; 
 # displays 15
 @array = (10, 20, 30);
 print "@array\n"; 
 # displays 10 20 30
 

+=

addition

It adds the right operand's value to the left operand.

Example:

 $v = 10;
 $v += 15;
 print "$v ( expected 25 )\n";
 

-=

subtraction

It subtracts the right operand from the left operand.

Example:

 $v = 25;
 $v -= 15;
 print "$v ( expected 10 )\n";
 

*=

multi-plication

It multiplies the left operand's value by the right operand's value.

Example:

 $v = 10; 
 $v *= 15;
 print "$v ( expected 150 )\n";
 

/=

division

It divides the left operand's value by the right operand's value.

Example:

 $v = 150;
 $v /= 15;
 print "$v ( expected 10) \n";
 

**=

expo-nentiation

It raises the left operand's value to the power of the right operand's value.

Example:

 $v = 12;
 $v **= 2;
 print "$v ( expected 144 ) \n";
 

%=

modulus

It divides the left operand value by the right operand value and assigns the remainder to the left operand.

Example:

 $v = 12;
 $v %= 5;
 print "$v ( expected 2 ) \n";
 
     

&&=

logical and

It's a combination between the logical "&&" and the assignment operators.

Example:

 $v = 1;
 $v &&= 7 == 7;
 print "$v ( expected 1 ) \n";
 

||=

logical or

It's a combination between the logical "||" and the assignment operators.

Example:

 $v = 0;
 $v ||= 7 == 7;
 print "$v ( expected 1 ) \n";
 
     

<<=

bitwise shift left

It's a bitwise left shift assign.

Example:

 $v = 25;
 $v <<= 3;
 print "$v ( expected 200 ) \n";
 

>>=

bitwise shift right

It's a bitwise right shift assign.

Example:

 $v = 32;
 $v >>= 3;
 print "$v ( expected 4 ) \n";
 

&=

bitwise and

It's a bitwise AND assign.

Example:

 $v = 32;
 $v &= 16;
 print "$v ( expected 0 ) \n";
 

|=

bitwise or

It's a bitwise OR assign.

Example:

 $v = 32;
 $v |= 16;
 print "$v ( expected 48 ) \n";
 

^=

bitwise exclusive or

It's a bitwise XOR assign.

Example:

 $v = 3;
 $v ^= 9;
 print "$v ( expected 10 ) \n";