**Summary**: in this tutorial, we are going to introduce you to some basic **Perl operators** including numeric operators, string operators and logical operators.

Table of Contents

## Numeric 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:

1 2 3 4 5 6 | #!/usr/bin/perl use warnings; use strict; print 10 + 20, "\n"; # 20 print 20 - 10, "\n"; # 10 |

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

1 2 3 4 5 6 7 | #!/usr/bin/perl use warnings; use strict; print 10 * 20, "\n"; # 200 print 20 / 10, "\n"; # 2 |

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:

1 | print 10 + 20/2 - 5 * 2 , "\n"; # 10 |

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:

1 | print (((10 + 20)/2 - 5) * 2); # 20; |

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:

1 2 3 4 5 6 7 | #!/usr/bin/perl use warnings; use strict; print 2**3, "\n"; # = 2 * 2 * 2 = 8. print 3**4, "\n"; # = 3 * 3 * 3 * 3 = 81. |

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:

1 2 3 4 5 6 | #!/usr/bin/perl use warnings; use strict; print 4 % 2, "\n"; # 0 even print 5 % 2, "\n"; # 1 odd |

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #!/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"; |

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:

Equality | Operators |
---|---|

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:

1 | $a <=> $b |

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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 | #!/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"; |

## String operators

### String comparison operators

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

Equality | Operators |
---|---|

Equal | eq |

Not Equal | ne |

Comparison | cmp |

Less than | lt |

Greater than | gt |

Less than or equal | le |

Greater than or equal | ge |

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

1 | print "This is" . " concatenation operator" . "\n"; |

The concatenation operator (.) combines two strings together.

A string can be repeated with the repetition ( `x`

) operator:

1 | print "a message " x 4, "\n"; |

### 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.

1 2 3 4 5 6 7 | #!/usr/bin/perl use warnings; use strict; my $s; chomp($s = <STDIN>); print $s; |

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

## Logical operators

Logical operators are often used in control statements such as if, while, given, 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`

.

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