Perl Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations, such as: 3+2=5.
Perl has a rich set of built-in operators. Let's look at some of the commonly used ones:
- Arithmetic Operators
- Comparison Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Quote Operators
- Miscellaneous Operators
- Operator Precedence
Arithmetic Operators
In the table examples, we set the variable $a to 10 and $b to 20.
Operator | Description | Example |
---|---|---|
+ | Addition | $a + $b results in 30 |
- | Subtraction | $a - $b results in -10 |
* | Multiplication | $a * $b results in 200 |
/ | Division | $b / $a results in 2 |
% | Modulus, the remainder after integer division | $b % $a results in 0 |
** | Exponentiation | $a**$b results in 10 to the power of 20 |
Example
#!/usr/bin/perl
$a = 10;
$b = 20;
print "\$a = $a , \$b = $b\n";
$c = $a + $b;
print '$a + $b = ' . $c . "\n";
$c = $a - $b;
print '$a - $b = ' . $c . "\n";
$c = $a * $b;
print '$a * $b = ' . $c . "\n";
$c = $a / $b;
print '$a / $b = ' . $c . "\n";
$c = $a % $b;
print '$a % $b = ' . $c. "\n";
$a = 2;
$b = 4;
$c = $a ** $b;
print '$a ** $b = ' . $c . "\n";
The above program execution output is:
$a = 10 , $b = 20
$a + $b = 30
$a - $b = -10
$a * $b = 200
$a / $b = 0.5
$a % $b = 10
$a ** $b = 16
Comparison Operators
In the table examples, we set the variable $a to 10 and $b to 20.
Operator | Description | Example |
---|---|---|
== | Checks if the values of two operands are equal, if yes then condition becomes true. | ($a == $b) is false |
!= | Checks if the values of two operands are equal, if values are not equal then condition becomes true. | ($a != $b) is true |
<=> | Checks if the values of two operands are equal, returns -1 if the left operand is less than the right operand, 0 if they are equal, and 1 if the left operand is greater. | ($a <=> $b) returns -1 |
> | Checks if the value of the left operand is greater than the value of the right operand, if yes then condition becomes true. | ($a > $b) returns false |
< | Checks if the value of the left operand is less than the value of the right operand, if yes then condition becomes true. | ($a < $b) returns true |
>= | Checks if the value of the left operand is greater than or equal to the value of the right operand, if yes then condition becomes true. | ($a >= $b) returns false |
<= | Checks if the value of the left operand is less than or equal to the value of the right operand, if yes then condition becomes true. | ($a <= $b) returns true |
Example
#!/usr/bin/perl
$a = 10;
$b = 20;
print "\$a = $a , \$b = $b\n";
if( $a == $b ){
print "$a == \$b is true\n";
}else{
print "\$a == \$b is false\n";
}
if( $a != $b ){
print "\$a != \$b is true\n";
}else{
print "\$a != \$b is false\n";
}
$c = $a <=> $b;
print "\$a <=> \$b returns $c\n";
if( $a > $b ){
print "\$a > \$b is true\n";
}else{
print "\$a > \$b is false\n";
}
if( $a >= $b ){
print "\$a >= \$b is true\n";
}else{
print "\$a >= \$b is false\n";
}
if( $a < $b ){
print "\$a < \$b is true\n";
}else{
print "\$a < \$b is false\n";
}
if( $a <= $b ){
print "\$a <= \$b is true\n";
}else{
print "\$a <= \$b is false\n";
}
The above program execution output is:
$a = 10 , $b = 20
$a == $b is false
$a != $b is true
$a <=> $b returns -1
$a > $b is false
$a >= $b is false
$a < $b is true
$a <= $b is true
In the following table example, we set the variable $a to "abc" and $b to "xyz", and then use comparison operators to calculate the result.
Operator | Description | Example |
---|---|---|
lt | Checks if the left string is less than the right string, if yes returns true, otherwise returns false. | ($a lt $b) returns true |
gt | Checks if the left string is greater than the right string, if yes returns true, otherwise returns false. | ($a gt $b) returns false |
le | Checks if the left string is less than or equal to the right string, if yes returns true, otherwise returns false. | ($a le $b) returns true |
ge | Checks if the left string is greater than or equal to the right string, if yes returns true, otherwise returns false. | ($a ge $b) returns false |
eq | Checks if the left string is equal to the right string, if yes returns true, otherwise returns false. | ($a eq $b) returns false |
ne | Checks if the left string is not equal to the right string, if yes returns true, otherwise returns false. | ($a ne $b) returns true |
cmp | Returns 1 if the left string is greater than the right string, 0 if they are equal, and -1 if the left string is less than the right string. | ($a cmp $b) returns -1 |
Example
#!/usr/bin/perl
$a = "abc";
$b = "xyz";
print "\$a = $a ,\$b = $b\n";
if( $a lt $b ){
print "$a lt \$b returns true\n";
}else{
print "\$a lt \$b returns false\n";
}
if( $a gt $b ){
print "\$a gt \$b returns true\n";
}else{
print "\$a gt \$b returns false\n";
}
if( $a le $b ){
print "\$a le \$b returns true\n";
}else{
print "\$a le \$b returns false\n";
}
if( $a ge $b ){
print "\$a ge \$b returns true\n";
}else{
print "\$a ge \$b returns false\n";
}
if( $a ne $b ){
print "\$a ne \$b returns true\n";
}else{
print "\$a ne \$b returns false\n";
}
$c = $a cmp $b;
print "\$a cmp \$b returns $c\n";
The above program execution output is:
$a = abc ,$b = xyz
abc lt $b returns true
$a gt $b returns false
$a le $b returns true
$a ge $b returns false
$a ne $b returns true
$a cmp $b returns -1
Assignment Operators
In the table examples, we set the variable $a to 10 and $b to 20.
Operator | Description | Example |
---|---|---|
= | Simple assignment operator, assigns values from right side operands to left side operand | $c = $a + $b will assign the value of $a + $b into $c |
+= | Add AND assignment operator, it adds the right operand to the left operand and assign the result to the left operand | $c += $a is equivalent to $c = $c + $a |
-= | Subtract AND assignment operator, it subtracts the right operand from the left operand and assigns the result to the left operand | $c -= $a is equivalent to $c = $c - $a |
*= | Multiply AND assignment operator, it multiplies the right operand with the left operand and assigns the result to the left operand | $c *= $a is equivalent to $c = $c * $a |
/= | Divide AND assignment operator, it divides the left operand with the right operand and assigns the result to the left operand | $c /= $a is equivalent to $c = $c / $a |
%= | Modulus AND assignment operator, it takes modulus using two operands and assigns the result to the left operand | $c %= $a is equivalent to $c = $c % a |
**= | Exponent AND assignment operator, performs exponential (power) calculation on operators and assigns value to the left operand | $c *= $a is equivalent to $c = $c * $a |
Example
#!/usr/bin/perl
$a = 10;
$b = 20;
print "\$a = $a ,\$b = $b\n";
$c = $a + $b;
print "After assignment \$c = $c\n";
$c += $a;
print "\$c = $c , after operation \$c += \$a\n";
$c -= $a;
print "\$c = $c , after operation \$c -= \$a\n";
$c *= $a;
print "\$c = $c , after operation \$c *= \$a\n";
$c /= $a;
print "\$c = $c , after operation \$c /= \$a\n";
$c %= $a;
print "\$c = $c , after operation \$c %= \$a\n";
$c = 2;
$a = 4;
print "\$a = $a , \$c = $c\n";
$c **= $a;
print "\$c = $c , after operation \$c **= \$a\n";
The above program execution output is:
$a = 10 ,$b = 20
After assignment $c = 30
$c = 40 , after operation $c += $a
$c = 30 , after operation $c -= $a
$c = 300 , after operation $c *= $a
$c = 30 , after operation $c /= $a
$c = 0 , after operation $c %= $a
$a = 4 , $c = 2
$c = 16 , after operation $c **= $a
Bitwise Operators
(The section on Bitwise Operators is not provided in the original text, so it is omitted here.) Bitwise operators act on bits and perform bit-by-bit operations.
Set $a = 60, $b = 13, and now they are represented in binary format as follows:
$a = 0011 1100
$b = 0000 1101
-----------------
$a&$b = 0000 1100
$a|$b = 0011 1101
$a^$b = 0011 0001
~$a = 1100 0011
The bitwise operators supported by Perl are shown in the following table:
Operator | Description | Example | ||
---|---|---|---|---|
& | Binary AND operator copies a bit to the result if it exists in both operands. | ($a & $b) will give 12, which is 0000 1100 | ||
Binary OR operator copies a bit if it exists in either operand. | ($a | $b) will give 61, which is 0011 1101 | ||
^ | Binary XOR operator copies a bit if it is set in one operand but not both. | ($a ^ $b) will give 49, which is 0011 0001 | ||
~ | Binary Ones Complement operator is unary and has the effect of 'flipping' bits. | (~$a ) will give -61, which is 1100 0011 in 2's complement form due to a signed binary number. | ||
<< | Binary Left Shift operator. The left operands value is moved left by the number of bits specified by the right operand. | $a << 2 will give 240, which is 1111 0000 | ||
>> | Binary Right Shift operator. The left operands value is moved right by the number of bits specified by the right operand. | $a >> 2 will give 15, which is 0000 1111 |
Example
#!/usr/bin/perl
use integer;
$a = 60;
$b = 13;
print "\$a = $a , \$b = $b\n";
$c = $a & $b;
print "\$a & \$b = $c\n";
$c = $a | $b;
print "\$a | \$b = $c\n";
$c = $a ^ $b;
print "\$a ^ \$b = $c\n";
$c = ~$a;
print "~\$a = $c\n";
$c = $a << 2;
print "\$a << 2 = $c\n";
$c = $a >> 2;
print "\$a >> 2 = $c\n";
The above program execution output is:
$a = 60 , $b = 13
$a & $b = 12
$a | $b = 61
$a ^ $b = 49
~$a = -61
$a << 2 = 240
$a >> 2 = 15
Logical Operators
The logical operators in Perl are shown in the following table.
In the table example, we set variable $a to true, $b to false.
Operator | Description | Example | ||||
---|---|---|---|---|---|---|
and | Logical AND operator. If both the operands are true, then the condition becomes true. | ($a and $b) is false. | ||||
&& | C-style logical AND operator. If both the operands are true, then the condition becomes true. | ($a && $b) is false. | ||||
or | Logical OR operator. If any of the two operands are non-zero, then the condition becomes true. | ($a or $b) is true. | ||||
C-style logical OR operator. If any of the two operands are non-zero, then the condition becomes true. | ($a | $b) is true. | ||||
not | Logical NOT operator. Used to reverse the logical state of its operand. If a condition is true, then the Logical NOT operator will make it false. | not($a and $b) is true. |
Example
#!/usr/bin/perl
$a = true;
$b = false;
print "\$a = $a , \$b = $b\n";
$c = ($a and $b);
print "\$a and \$b = $c\n";
$c = ($a && $b);
print "\$a && \$b = $c\n";
$c = ($a or $b);
print "\$a or \$b = $c\n";
$c = ($a || $b);
print "\$a || \$b = $c\n";
$a = 0;
$c = not($a);
print "not(\$a)= $c\n";
The above program execution output is:
$a = true , $b = false
$a and $b = false
$a && $b = false
$a or $b = true
$a || $b = true
not($a)= 1
Quote-like Operators
The quote-like operators in Perl are shown in the following table.
Operator | Description | Example |
---|---|---|
q{ } | Encloses the string within single quotes | q{abcd} results in 'abcd' |
qq{ } | Encloses the string within double quotes | qq{abcd} results in "abcd" |
qx{ } | Encloses the string within backticks | qx{abcd} results in abcd |
Example
#!/usr/bin/perl
$a = 10;
$b = q{a = $a};
print "q{a = \$a} = $b\n";
$b = qq{a = $a};
print "qq{a = \$a} = $b\n";
# Execute using the Unix date command
$t = qx{date};
print "qx{date} = $t\n";
The above program execution output is:
q{a = $a} = a = $a
qq{a = $a} = a = 10
qx{date} = Fri Jun 10 16:22:33 CST 2016
Miscellaneous Operators
Besides the operators mentioned above, Perl also supports the following operators:
Operator | Description | Example |
---|---|---|
. | Dot (.) is used to concatenate two strings. | If $a="run", $b="oob", $a.$b results in "tutorialpro" |
x | The x operator returns the string repeated the number of times specified. | ('-' x 3) results in ---. |
.. | Range operator. | (2..5) results in (2, 3, 4, 5) |
++ | Auto-increment operator, increases integer value by one | $a =10, $a++ results in 11 |
-- | Auto-decrement operator, decreases integer value by one | $a =10, $a-- results in 9 |
-> | Arrow operator is used to specify a method of a class | $obj->$a indicates method $a of object $obj. |
Example
#!/usr/bin/perl
$a = "run";
$b = "oob";
print "\$a = $a , \$b = $b\n";
$c = $a . $b;
print "\$a . \$b = $c\n";
$c = "-" x 3;
print "\"-\" x 3 = $c\n";
@c = (2..5);
print "(2..5) = @c\n";
$a = 10;
$b = 15;
print "\$a = $a , \$b = $b\n";
$a++;
$c = $a ;
print "\$a 执行 \$a++ = $c\n";
$b--;
$c = $b ;
print "\$b 执行 \$b-- = $c\n";
The above program execution output is:
$a = run , $b = oob
$a . $b = tutorialpro
"-" x 3 = ---
(2..5) = 2 3 4 5
$a = 10 , $b = 15
$a 执行 $a++ = 11
$b 执行 $b-- = 14
Operator Precedence
The following table lists the precedence of operators in Perl:
Operator | Associativity | ||
---|---|---|---|
++, -- | None | ||
-, ~, ! | Right to Left | ||
** | Right to Left | ||
=~, !~ | Left to Right | ||
*, /, %, x | Left to Right | ||
+, -, . | Left to Right | ||
<<, >> | Left to Right | ||
-e, -r, | None | ||
<, <=, >, >=, lt, le, gt, ge | Left to Right | ||
==, !=, <=>, eq, ne, cmp | Left to Right | ||
& | Left to Right | ||
, ^ | Left to Right | ||
&& | Left to Right | ||
Left to Right | |||
.. | Left to Right | ||
? and : | Right to Left | ||
=, +=, -=, *=, | Right to Left | ||
, | Left to Right | ||
not | Left to Right | ||
and | Left to Right | ||
or, xor | Left to Right |
Example
#!/usr/bin/perl
$a = 20;
$b = 10;
$c = 15;
$d = 5;
$e;
print "\$a = $a, \$b = $b, \$c = $c ,\$d = $d\n";
$e = ($a + $b) * $c / $d;
print "(\$a + \$b) * \$c / \$d = $e\n";
$e = (($a + $b) * $c )/ $d;
print "((\$a + \$b) * \$c) / \$d = $e\n";
$e = ($a + $b) * ($c / $d);
print "(\$a + \$b) * (\$c / \$d ) = $e\n";
$e = $a + ($b * $c ) / $d;
print "\$a + (\$b * \$c )/ \$d = $e\n";
The above program execution output is:
$a = 20, $b = 10, $c = 15 ,$d = 5
($a + $b) * $c / $d = 90
(($a + $b) * $c) / $d = 90
($a + $b) * ($c / $d ) = 90
$a + ($b * $c )/ $d = 50