Updated March 13, 2023
What Perl is?
Perl is basically a language that is used in creating dynamic websites, System Administration, text processing. It was designed by Larry Perl 31 years ago on December 18, 1987. Let’s start with the operators in Perl.
Perl Operators
Just like any other language, the operators in Perl can be categorized in the following categories:
- Arithmetic Operators
- Equality Operators
- Assignment Operators
- Bitwise Operators
- Logical Operators
- Quote-like Operators
- Miscellaneous Operators
So, let’s go through Perl operators one by one:
1. Arithmetic Operators
As the name suggests Arithmetic Operators are used to doing arithmetic operations like subtraction addition etc.
So let’s take two operands a and b with values $a =10, $b =30
- + (Addition): It used to add values on either side of the addition operator: $a + $b =40
- – (Subtraction): It is used to subtract right hand side from left hand side: $b – $a =10
- * (Multiplication): It is used to multiply values on either side of the operator $a * $b =300
- / (Division): It is used to divide the left-hand operand by the right-hand operand $b / $a = 3
- % (Modulus): It is used to divide the left-hand operand by the right-hand operand and return remainder $b % $a = 0
- ** (Exponential): It is used to perform power calculation $b ** $a gives 30 raised to power 10.
2. Equality Operators
These are called relational operator so let’s keep the values of both a and b same as they were in the case of arithmetic operators:
- == (equal to): As the name suggests, checks if the value of two operands is equal or not if they are equal it becomes true. In this case $a == $b is not true.
- != (not equal to): As the name suggests, checks if the value of two operands is equal or not if they are not equal it becomes true. In this case $a != $b is true
- > (Greater than): This operator checks if the value of two operands is greater than each other or not $a > $b is not true.
- < (Less than): This operator checks if the value of two operands is less than each other or not $a < $b is true.
- >= (Greater than equal to): This operator checks that if the value of two operands is greater than or equal to each other. In our case $a >= $b is not true.
- <= (Less than equal to): This operator checks that if the value of two operands is less than or equal to each other. In our case $a <= $b is true.
Now let’s check string equality operators in Perl, let’s change value as $a =”nil” and $b = “abc”
- It: It checks if the left wise string argument is less than the right wise string argument. In our case, $a It $b is not true.
- gt: It checks if the left wise string argument is greater than the right wise string argument. In our case, $a gt $b is true.
- le: It checks if the left wise string argument is less than or equal to the right wise string argument. In our case, $a Ie $b is false
- ge: It checks if the left wise string argument is greater than or equal to the right wise string argument. In our case, $a ge $b is false.
3. Assignment Operators
Let’s change the value of a and b to previous values of 10 and 30. Perl supports the following Assignment operators:
- =: It is an assignment operator. It assigns the value from the right-hand side to the left-hand side, for example, $c = $a + $ b which makes the value of c to 40.
- +=: It is called add AND assignment operator. It adds the right operand to left operand and assigns the value of the result to left operand.
- -=: It is called Subtract AND assignment operator. It subtracts the right operand from left operand and assigns the value of the result to left operand.
- *=: It is called multiple AND assignment operator. It multiplies the right operand from left operand and assigns the value of the result to the left operand.
- /=: It is called Divide AND assignment operator. It divides the right operand from left operand and assigns the value of the result to left operand.
4. Bitwise Operators
Perl supports the following bitwise operators:
- & (Binary and): It copies bits to result which are in both operands.
- | (Binary OR): It copies bits to result which are in either operand.
- ^ (Binary XOR): It copies a bit if it is set in one operand, not both.
5. Logical Operators
Perl contains the following logical operators:
- And (Logical AND): If both operands become true then the operator returns true.
- OR (Logical OR): If any of the operands is non-zero then it becomes true.
- Not (Logical NOT): It reverses the logical state of the operand.
6. Quota Like Operators
Perl supports the following Quota Like operators:
- q{}: It encloses a string in single quotes. for example q{nil} becomes ‘nil’.
- qq{}: It encloses a string in double-quotes. for example qq{nil} becomes “nil”.
- qx{}: It encloses a string in reverse quotes.
7. Miscellaneous Operators
Perl contains the following Miscellaneous operators:
- . (Binary Operator dot): It is used to concatenate two strings. If $a =”nil” and $b = “def” $a.$b =”nildef”.
- x (Repetition Operator): It returns a string of repeated left side operand. The number of repetitions is specified by the right-hand side operand. For example: (‘-‘ x 3) gives ‘—‘.
- ++ (Auto increment Operator): It will increase the value by one. The value must be an integer. For example: if $a =10 $a++ gives 11.
- — (Auto decrement Operator): It will decrease the value by one. The value must be an integer. For example: if $a =10 $a– will give 9.
Recommended Articles
This has been a guide to Perl Operators. Here we have discuss 7 different types of Perl Operators along with respective examples. You can also go through our other suggested articles to learn more –