Perl operator


Release date:2023-10-18 Update date:2023-10-21 Editor:admin View counts:307

Label:

Perl operator

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

Perl language comes with a wealth of operators, so let’s take a lookat a few commonly used ones:

  • Arithmetic operator

  • Comparison operator

  • Logical operator

  • Assignment operator

  • Bit operator

  • Quotation mark operator

  • Other operators

  • Operator precedence

Arithmetic operator

In the table instance, we set the variable $a as 10 and $b as 20.

Operator

Description

Example

+

Addition operation

$a + $b results in 30

-

Subtraction operation

$a-$b results in-10

*

Multiplication operation

$a * $b the result is 200

/

Division operation

$b / $a result is 2

%

Remainder operation, remainder after division

$b $a result is 0

**

Multiplying power

The result is 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 output of the above program execution is as follows:

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

Comparison operator

In the table instance, we set the variable $a as 10 and $b as 20.

Operator

Description

Example

==

Check whether the values of the two operands are equal, and if so, the condition is true, otherwise false.

($a = = $b) is false

! =

Check whether the values of the two operands are equal, and if not, the condition is true, otherwise false.

($a! = $b) is true.

< = >

Check whether the values of the two operands are equal, return-1 if the number on the left is less than the number on the right, 0 if the number on the left is greater than the number on the right, return 1 if the number on the left is greater than that on the right.

($a < = > $b) returns-1.

>

Check whether the value of the left Operand is greater than the value of theright Operand. If so, the condition is true, otherwise it is false.

($a > $b) returns false.

<

Check whether the value of the left Operand is less than the value of the right Operand, and if so, the condition is true, otherwise false is returned.

($a < $b) returns true.

>=

Check whether the value of the left Operand is greater than or equal to the value of the right Operand, and if so, the condition is true, otherwise false is returned.

($a > = $b) returns false.

<=

Check whether the value of the left Operand is less than or equal to the value of the right Operand, and if so, the condition is true, otherwise false is returned.

($a < = $b) returns true.

Example

#/ Usr/bin/perl $a=10$ B=20; Print " $a=$a, $b
=$b n "; if ($a==$b) {print" $a== $b result true n ";}else {print" $a== $b
Result false n ";} if ($a!=$b) {print" $a!= $b Result
True n ";} else {print" $a!= $b result false n ";} $c=$a<=>$b; print" $a
<=> $b returns the result of $c n "; if ($a>$b) {print" $a> $b
True n ";}else {print" $a> $b result false n ";} if ($a>=$b) {print" $a
>=\\$b result true n ";}else {print" $a>= $b result
False n ";} if ($a<$b) {print" $a< $b result true n ";}else {print" $a
< $bResult false n ";} if ($a<=$b) {print" $a<= $bResult
True n ";} else {print" $a<= $b result false n ";}

The output of the above program execution is as follows:

$a=10, $b=20
$a==$b result false
$a!=$ B Result true
$a<=>$b returns -1
$a>$b result false
$a>=$b result false
$a<$b result true
$a<=$b result true

In the following table example, set the variable $a to "abc" and $b to "xyz", and then use a comparison operator to calculate the result.

Operator

Description

Example

Lt

Check whether the string on the left is less than the string on the right, and return true if not, false otherwise.

($a lt $b) returns true.

Gt

Check whether the string on the left is larger than the string on the right, and return true if not, false otherwise.

($a gt $b) returns false.

Le

Check whether the string on the left is less than or equal to the string on the right, and return true if not, false otherwise.

($a le $b) returns true

Ge

Check whether the string on the left is greater than or equal to the string on the right, and return true if not, false otherwise.

($a ge $b) returns false.

Eq

Check whether the string on the left is equal to the string on the right, and return true if not, false otherwise.

($an eq $b) returns false.

Ne

Check whether the string on the left is not equal to the string on the right, and return true if not, false otherwise.

($a ne $b) returns true

Cmp

If the string on the left is greater than the string on the right, it returns 1, if equality returns 0, and if the string on the left is less thanthe string on the right, it returns-1.

($a cmp $b) returns-1.

Example

#/ Usr/bin/perl $a="abc"$ B="xyz"; Print " $a=$a, $b
=$b n "; if ($alt $b) {print" $alt b returns true n ";}else {print" $a lt $b
Return false n ";} if ($agt $b) {print" $a gt $b Return
True n ";}else {print" $a gt $b returns false n ";} if ($ale $b) {print" $a
Le $b returns true n ";}else {print" $a le $b returns
False n ";} if ($age $b) {print" $a ge $b returns true n ";}else {print" $a
Ge $b returns false n ";} if ($ane $b) {print" $a ne $b returns
True n ";} else {print" $a ne $b returns false n ";} c=$acmp $b; print" $a
Cmp $b returns $c n ";

The output of the above program execution is as follows:

$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 operator

In the table instance, we set the variable $a as 10 and $b as 20.

Operator

Description

Example

=

A simple assignment operator that assigns the value of the right Operand to the left Operand

$c = $a + $b Will put $a + $b the value assigned to the $c

+=

Add the assignment operator to assign the result of the right Operand plus the left Operand to the left Operand

$c += $a equal to $c = $c + $a

-=

Subtract and assign the left Operand to the left Operand minus the result ofthe right Operand

$c -= $a equal to $c = $c - $a

*=

Multiply and assign the operator to assign the result of the right Operand multiplied by the left Operand to the left Operand

$c *= $a equal to $c = $c * $a

/=

Divide and assign the operator to assign the result of dividing the left Operand by the right Operand to the left Operand

$c /= $a equal to $c = $c / $a

%=

Find the module and assignment operator, find the module assignment of two operands to the left Operand

$c %= $a equal to $c = $c % a

**=

Power and assignment operator to assign the power of two operands to the left Operand

$c **= $a equal 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, operation statement $c+= $a n "; $c -=$a; print" $c=$c, operation statement $c
-=\\$a n "; $c *=$a; print" $c=$c, operation statement $c *= $a n "; $c/=$a;
Print " $c=$c, operation statement $c/= $a n"$ C%=$a;
Print " $c=$c, arithmetic statement $c%= $a n"$ C=2$ A=4; Print " $a=$a,
\\$c=$c n "; $c * *=$a; print" $c=$c, arithmetic statement $c * *= $a n ";

The output of the above program execution is as follows:

$a=10, $b=20
After assignment, $c=30
$c=40, operation statement $c+=$a
$c=30, operation statement $c -=$a
$c=300, operation statement $c *=$a
$c=30, operation statement $c/=$a
$c=0, operation statement $c%=$a
$a=4, $c=2
$c=16, operation statement $c * *=$a

Bit operation

The bit operator acts on the bit and performs the operation bit by bit.

Set $a = 60 and $b = 13, now 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

Perl supported bit operators are shown in the following table:

Operator

Description

Example

&

If it exists in both operands, the binary AND operator copies a bit to the result.

($a & $b) will get 12 with a binary of 0000 1100


If it exists in any Operand, the binary OR operator copies a bit into the result.

($a | $b) will get 61 with a binary of 0011 1101

^

If it exists in one of the operands but not in both operands, the binary XORoperator copies a bit to the result.

($a ^ $b) will get 49, with a binary of 0011 0001

~

The binary anti-code operator is a unary operator with a “flip” bit effect, that is, 0 becomes 1, 1 becomes 0.

(~ $a) will get-61, the binary is 1100 0011, the inverse form of a signed binary number.

< <

Binary left shift operator. The value of the left Operand moves the number of digits specified by the right Operand to the left.

$a < < 2 will get 240. the binary is 1111 0000.

> >

Binary right shift operator. The value of the left Operand moves the number of digits specified by the right Operand to the right.

$a > > 2 will get 15, binary 0000 1111

Example

#!/usr/bin/perluse 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 output of the above program execution is as follows:

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

Logical operator

Perl logical operators are shown in the following table.

In the table instance, we set the variable $a as true and $b as false.

Operator

Description

Example

and

Logic and operators. If both operands are true, the condition is true.

($an and $b) is false.

&&

C-style logic and operators. If both operands are true, the condition is true

($a & & $b) is false.

or

Logic or operator. If either of the operands is nonzero, the condition is true.

($an or $b) is true.

||

C style logic or operator. If either of the operands is nonzero, the condition is true.

($a || $b) is true.

not

Logical non-operator. Used to reverse the logical state of operands. If the condition is true, the logical non-operator will make it false.

Not ($an and $b) is true.

Example

#!/usr/bin/perl$a=true;$b=false;print"\\$a =$a,\\$b
=$b\\n";$c=($aand$b);print"\\$a and\\$b =$c\\n";$c=($a&&$b);print"\\$a
&&\\$b =$c\\n";$c=($aor$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 output of the above program execution is as follows:

$a = true , $b = false
$a and $b = false
$a && $b = false
$a or $b = true
$a || $b = true
not($a)= 1

Quotation mark operation

Perl quote operators are shown in the following table.

Operator

Description

Example

Q{}

Add single quotation marks to a string

Q {abcd} result is’ abcd’

Qq{}

Add double quotation marks to a string

Qq {abcd} result is “abcd”

Qx{}

Add backquotes to a string

Qx {abcd} result isabcd

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";# Executing the date command using
unix $t=qx{date};
print "qx{date}=$t\\n";

The output of the above program execution is as follows:

Q {a=$a}=a=$a
Qq {a=$a}=a=10
Qx {date}=Friday, June 10, 2016 16:22:33 CST

Other operators

In addition to the operators we mentioned above Perl The following operators are also supported:

Operator

Description

Example

.

Dot (.) Used to concatenate two strings.

If $a = “run”, $b = “oob”, and $a.accounb results in “runoob”

X

The x operator returns the number of times the string is repeated.

(‘-‘x 3) the output is -.

(2. 5) the output is (2, 3, 4, 5)

++

Self-increment operator, the integer value increases by 1

$a = 10, $asides + output to 11

Self-subtraction operator, integer value minus 1

$a = 10, $aMel-output to 9

->

The arrow is used to specify the method of a class

$obj- > $a represents the $a method of the 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="-"x3;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 implement\\$a++ =$c\\n";$b--;$c=$b;print"\\$b
implement \\$b-- =$c\\n";

The output of the above program execution is as follows:

$a  = run , $b = oob
$a . $b = runoob
"-" x 3 = ---
(2..5) = 2 3 4 5
$a  = 10 , $b = 15
$a implement $a++ = 11
$b implement $b-- = 14

Operator precedence

The following table lists Perl operator precedence of the language:

Operator

Binding property

++,?--

None

-,?~,?!

From right to left

**

From right to left

=~,?!~

From left to right

*,?/,?%,?x

From left to right

+,?-,?.

From left to right

<<,?>>

From left to right

-e,?-r,

None

<,?<=,?>,?>=,?lt,?le,?gt,?ge

From left to right

==,?!=,?<=>,?eq,?ne,?cmp

From left to right

&

From left to right

|,?^

From left to right

&&

From left to right

||

From left to right

..

From left to right

??and?:

From right to left

=,?+=,?-=,?*=,

From right to left

Others

,

From left to right

not

From left to right

and

From left to right

or,?xor

From 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 output of the above program execution is as follows:

$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

Powered by TorCMS (https://github.com/bukun/TorCMS).