YouTip LogoYouTip

Perl Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical operations, for example: 3+2=5.

\n\n

Perl has a rich set of built-in operators. Let's look at some of the common ones:

\n\n\n\n
\n\n

Arithmetic Operators

\n\n

In the table examples, we set the variable $a to 10 and $b to 20.

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
OperatorDescriptionExample
+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 (remainder of integer division)$b % $a results in 0
**Exponentiation$a**$b results in 10 to the power of 20
\n\n

Example

\n\n
#!/usr/bin/perl\n$a = 10;\n$b = 20;\nprint "$a = $a , $b = $bn";\n$c = $a + $b;\nprint '$a + $b = ' . $c . "n";\n$c = $a - $b;\nprint '$a - $b = ' . $c . "n";\n$c = $a * $b;\nprint '$a * $b = ' . $c . "n";\n$c = $a / $b;\nprint '$a / $b = ' . $c . "n";\n$c = $a % $b;\nprint '$a % $b = ' . $c . "n";\n$a = 2;\n$b = 4;\n$c = $a ** $b;\nprint '$a ** $b = ' . $c . "n";
\n\n

The output of the above program is:

\n\n
$a = 10 , $b = 20\n$a + $b = 30\n$a - $b = -10\n$a * $b = 200\n$a / $b = 0.5\n$a % $b = 10\n$a ** $b = 16
\n\n
\n\n

Comparison Operators

\n\n

In the table examples, we set the variable $a to 10 and $b to 20.

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
OperatorDescriptionExample
==Checks if the values of two operands are equal. If yes, the condition is true, otherwise false.($a == $b) is false.
!=Checks if the values of two operands are not equal. If yes, the condition is true, otherwise false.($a != $b) is true.
<=>Checks if the values of two operands are equal. Returns -1 if the left operand is less than the right, 0 if equal, and 1 if the left operand is greater than the right.($a <=> $b) returns -1.
>Checks if the value of the left operand is greater than the right. If yes, the condition is true, otherwise false.($a > $b) returns false.
<Checks if the value of the left operand is less than the right. If yes, the condition is true, otherwise false.($a < $b) returns true.
>=Checks if the value of the left operand is greater than or equal to the right. If yes, the condition is true, otherwise false.($a >= $b) returns false.
<=Checks if the value of the left operand is less than or equal to the right. If yes, the condition is true, otherwise false.($a <= $b) returns true.
\n\n

Example

\n\n
#!/usr/bin/perl\n$a = 10;\n$b = 20;\nprint "$a = $a , $b = $bn";\nif($a == $b){\n    print "$a == $b Result truen";\n}else{\n    print "$a == $b Result false";\n}\nif($a != $b){\n    print "$a != $b Result truen";\n}else{\n    print "$a != $b Result false";\n}\n$c = $a <=> $b;\nprint "$a <=> $b Return $cn";\nif($a > $b){\n    print "$a > $b Result truen";\n}else{\n    print "$a > $b Result false";\n}\nif($a >= $b){\n    print "$a >= $b Result truen";\n}else{\n    print "$a >= $b Result false";\n}\nif($a < $b){\n    print "$a < $b Result truen";\n}else{\n    print "$a < $b Result false";\n}\nif($a <= $b){\n    print "$a <= $b Result truen";\n}else{\n    print "$a <= $b Result false";\n}
\n\n

The output of the above program is:

\n\n
$a = 10 , $b = 20\n$a == $b Result false\n$a != $b Result true\n$a <=> $b Return -1\n$a > $b Result false\n$a >= $b Result false\n$a < $b Result true\n$a <= $b Result true
\n\n

In the following table examples, we set the variable $a to "abc" and $b to "xyz", then use comparison operators to calculate the results.

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
OperatorDescriptionExample
ltChecks if the left string is less than the right. If yes, returns true, otherwise false.($a lt $b) returns true.
gtChecks if the left string is greater than the right. If yes, returns true, otherwise false.($a gt $b) returns false.
leChecks if the left string is less than or equal to the right. If yes, returns true, otherwise false.($a le $b) returns true.
geChecks if the left string is greater than or equal to the right. If yes, returns true, otherwise false.($a ge $b) returns false.
eqChecks if the left string is equal to the right. If yes, returns true, otherwise false.($a eq $b) returns false.
neChecks if the left string is not equal to the right. If yes, returns true, otherwise false.($a ne $b) returns true.
cmpReturns 1 if the left string is greater than the right, 0 if equal, and -1 if the left string is less than the right.($a cmp $b) returns -1.
\n\n

Example

\n\n
#!/usr/bin/perl\n$a = "abc";\n$b = "xyz";\nprint "$a = $a ๏ผŒ$b = $bn";\nif($a lt $b){\n    print "$a lt $b Return truen";\n}else{\n    print "$a lt $b Return falsen";\n}\nif($a gt $b){\n    print "$a gt $b Return truen";\n}else{\n    print "$a gt $b Return falsen";\n}\nif($a le $b){\n    print "$a le $b Return truen";\n}else{\n    print "$a le $b Return falsen";\n}\nif($a ge $b){\n    print "$a ge $b Return truen";\n}else{\n    print "$a ge $b Return falsen";\n}\nif($a ne $b){\n    print "$a ne $b Return truen";\n}else{\n    print "$a ne $b Return falsen";\n}\n$c = $a cmp $b;\nprint "$a cmp $b Return $cn";
\n\n

The output of the above program is:

\n\n
$a = abc ๏ผŒ$b = xyz\nabc lt $b Return true\n$a gt $b Return false\n$a le $b Return true\n$a ge $b Return false\n$a ne $b Return true\n$a cmp $b Return -1
\n\n
\n\n

Assignment Operators

\n\n

In the table examples, we set the variable $a to 10 and $b to 20.

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
OperatorDescriptionExample
=Simple assignment operator. Assigns the value from the right operand to the left operand.$c = $a + $b assigns the value of $a + $b to $c
+=Add and assignment operator. Adds the right operand to the left operand and assigns the result to the left operand.$c += $a is equivalent to $c = $c + $a
-=Subtract and assignment operator. 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. 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. Divides the left operand by the right operand and assigns the result to the left operand.$c /= $a is equivalent to $c = $c / $a
%=Modulus and assignment operator. Takes modulus using two operands and assigns the result to the left operand.$c %= $a is equivalent to $c = $c % a
**=Exponentiation and assignment operator. Raises the left operand to the power of the right operand and assigns the result to the left operand.$c **= $a is equivalent to $c = $c ** $a
\n\n

Example

\n\n
#!/usr/bin/perl\n$a = 10;\n$b = 20;\nprint "$a = $a ๏ผŒ$b = $bn";\n$c = $a + $b;\nprint "After assignment $c = $cn";\n$c += $a;\nprint "$c = $c ๏ผŒExpression statement $c += $an";\n$c -= $a;\nprint "$c = $c ๏ผŒExpression statement $c -= $an";\n$c *= $a;\nprint "$c = $c ๏ผŒExpression statement $c *= $an";\n$c /= $a;\nprint "$c = $c ๏ผŒExpression statement $c /= $an";\n$c %= $a;\nprint "$c = $c ๏ผŒExpression statement $c %= $an";\n$c = 2;\n$a = 4;\nprint "$a = $a ๏ผŒ $c = $cn";\n$c **= $a;\nprint "$c = $c ๏ผŒExpression statement $c **= $an";
\n\n

The output of the above program is:

\n\n
$a = 10 ๏ผŒ$b = 20\nAfter assignment $c = 30\n$c = 40 ๏ผŒExpression statement $c += $a\n$c = 30 ๏ผŒExpression statement $c -= $a\n$c = 300 ๏ผŒExpression statement $c *= $a\n$c = 30 ๏ผŒExpression statement $c /= $a\n$c = 0 ๏ผŒExpression statement $c %= $a\n$a = 4 ๏ผŒ $c = 2\n$c = 16 ๏ผŒExpression statement $c **= $a
\n\n
\n\n

Bitwise Operators

\n\n

Bitwise operators work on bits and perform bit-by-bit operations.

\n\n

Set $a = 60, $b = 13. In binary format, they are shown as follows:

\n\n
$a = 0011 1100\n$b = 0000 1101\n-----------------\n$a & $b = 0000 1100\n$a | $b = 0011 1101\n$a ^ $b = 0011 0001\n~$a = 1100 0011
\n\n

Perl supports the following bitwise operators:

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
OperatorDescriptionExample
&Binary AND operator. Copies a bit to the result if it exists in both operands.($a & $b) will give 12, binary 0000 1100
|Binary OR operator. Copies a bit to the result if it exists in either operand.($a | $b) will give 61, binary 0011 1101
^Binary XOR operator. Copies a bit to the result if it exists in one operand but not both.($a ^ $b) will give 49, binary 0011 0001
~Binary Ones Complement operator. A unary operator that "flips" bits, i.e., 0 becomes 1 and 1 becomes 0.(~$a) will give -61, binary 1100 0011, which is the two's complement form of a signed binary number.
<<Binary Left Shift operator. The left operand's value is shifted left by the number of bits specified by the right operand.$a << 2 will give 240, binary 1111 0000
>>Binary Right Shift operator. The left operand's value is shifted right by the number of bits specified by the right operand.$a >> 2 will give 15, binary 0000 1111
\n\n

Example

\n\n
#!/usr/bin/perl\nuse integer;\n$a = 60;\n$b = 13;\nprint "$a = $a , $b = $bn";\n$c = $a & $b;\nprint "$a & $b = $cn";\n$c = $a | $b;\nprint "$a | $b = $cn";\n$c = $a ^ $b;\nprint "$a ^ $b = $cn";\n$c = ~$a;\nprint "~$a = $cn";\n$c = $a << 2;\nprint "$a << 2 = $cn";\n$c = $a >> 2;\nprint "$a >> 2 = $cn";
\n\n

The output of the above program is:

\n\n
$a = 60 , $b = 13\n$a & $b = 12\n$a | $b = 61\n$a ^ $b = 49\n~$a = -61\n$a << 2 = 240\n$a >> 2 = 15
\n\n
\n\n

Logical Operators

\n\n

Perl logical operators are shown in the following table.

\n\n

In the table examples, we set the variable $a to true and $b to false.

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
OperatorDescriptionExample
andLogical AND operator. If both operands are true, the condition is true.($a and $b) is false.
&&C-style Logical AND operator. If both operands are true, the condition is true.($a && $b) is false.
orLogical OR operator. If any of the two operands is non-zero, the condition is true.($a or $b) is true.
||C-style Logical OR operator. If any of the two operands is non-zero, the condition is true.($a || $b) is true.
notLogical NOT operator. Used to reverse the logical state of its operand. If the condition is true, the logical NOT operator will make it false.not($a and $b) is true.
\n\n

Example

\n\n
#!/usr/bin/perl\n$a = true;\n$b = false;\nprint "$a = $a , $b = $bn";\n$c = ($a and $b);\nprint "$a and $b = $cn";\n$c = ($a && $b);\nprint "$a && $b = $cn";\n$c = ($a or $b);\nprint "$a or $b = $cn";\n$c = ($a || $b);\nprint "$a || $b = $cn";\n$a = 0;\n$c = not($a);\nprint "not($a)= $cn";
\n\n

The output of the above program is:

\n\n
$a = true , $b = false\n$a and $b = false\n$a && $b = false\n$a or $b = true\n$a || $b = true\nnot($a)= 1
\n\n
\n\n

Quote Operators

\n\n

Perl quote operators are shown in the following table.

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
OperatorDescriptionExample
q{ }Adds single quotes to a string.q{abcd} results in 'abcd'
qq{ }Adds double quotes to a string.qq{abcd} results in "abcd"
qx{ }Adds backticks to a string.qx{abcd} results in `abcd`
\n\n

Example

\n\n
#!/usr/bin/perl\n$a = 10;\n$b = q{a = $a};\nprint "q{a = $a} = $bn";\n$b = qq{a = $a};\nprint "qq{a = $a} = $bn";\n$t = qx{date};\nprint "qx{date} = $tn";
\n\n

The output of the above program is:

\n\n
q{a = $a} = a = $a\nqq{a = $a} = a = 10\nqx{date} = 2016Friday, June 10, 16:22:33 CST
\n\n
\n\n

Miscellaneous Operators

\n\n

In addition to the operators mentioned above, Perl also supports the following operators:

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
OperatorDescriptionExample
.The dot (.) is used to concatenate two strings.If $a="run", $b="oob", then $a.$b results in ""
xThe x operator returns the string repeated a specified number of times.('-' x 3) outputs ---.
..The .. is the range operator.(2..5) outputs (2, 3, 4, 5)
++Increment operator. Increases an integer value by 1.$a = 10, $a++ outputs 11
--Decrement operator. Decreases an integer value by 1.$a = 10, $a-- outputs 9
->The arrow is used to specify a method of a class.$obj->$a denotes the $a method of object $obj.
\n\n

Example

\n\n
#!/usr/bin/perl\n$a = "run";\n$b = "oob";\nprint "$a = $a ๏ผŒ $b = $bn";\n$c = $a . $b;\nprint "$a . $b = $cn";\n$c = "-" x 3;\nprint ""-" x 3 = $cn";\n@c = (2..5);\nprint "(2..5) = @cn";\n$a = 10;\n$b = 15;\nprint "$a = $a ๏ผŒ $b = $bn";\n$a++;\n$c = $a;\nprint "$a Execute $a++ = $cn";\n$b--;\n$c = $b;\nprint "$b Execute $b-- = $cn";
\n\n

The output of the above program is:

\n\n
$a = run ๏ผŒ $b = oob\n$a . $b = \n"-" x 3 = ---\n(2..5) = 2 3 4 5\n$a = 10 ๏ผŒ $b = 15\n$a Execute $a++ = 11\n$b Execute $b-- = 14
\n\n
\n\n

Operator Precedence

\n\n

The following table lists the operator precedence in Perl:

\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
OperatorAssociativity
++,--Non-associative
-,~,!Right to left
**Right to left
=~,!~Left to right
*,/,%,xLeft to right
+,-,.Left to right
<<,>>Left to right
-e,-r,Non-associative
<,<=,>,>=,lt,le,gt,geLeft to right
==,!=,<=>,eq,ne,cmpLeft to right
&Left to right
|,^Left to right
&&Left to right
||Left to right
..Left to right
? and :Right to left
=,+=,-=,*=,Right to left
Others
,Left to right
notLeft to right
andLeft to right
or,xorLeft to right
\n\n

Example

\n\n
#!/usr/bin/perl\n$a = 20;\n$b = 10;\n$c = 15;\n$d = 5;\n$e;\nprint "$a = $a, $b = $b, $c = $c ๏ผŒ$d = $dn";\n$e = ($a + $b) * $c / $d;\nprint "($a + $b) * $c / $d = $en";\n$e = (($a + $b) * $c) / $d;\nprint "(($a + $b) * $c) / $d = $en";\n$e = ($a + $b) * ($c / $d);\nprint "($a + $b) * ($c / $d ) = $en";\n$e = $a + ($b * $c) / $d;\nprint "$a + ($b * $c )/ $d = $en";
\n\n

The output of the above program is:

\n\n
$a = 20, $b = 10, $c = 15 ๏ผŒ$d = 5\n($a + $b) * $c / $d = 90\n(($a + $b) * $c) / $d = 90\n($a + $b) * ($c / $d ) = 90\n$a + ($b * $c )/ $d = 50
โ† Bootstrap5 Button GroupsBootstrap5 Buttons โ†’