SoFunction
Updated on 2025-04-10

Perl operator detailed description

1. Arithmetic operators: +(add), -(subtraction), *(multiple), /(division), **(power), %(take the remainder), -(monograph negative)
(1) The cardinality of the power cannot be negative, such as (-5) ** 2.5 # error;
(2) The power result cannot exceed the limit of computer representation, such as 10 ** 999999 # error
(3) If the remaining operand is not an integer, round it into an integer and then perform it; the right side of the operator cannot be zero
(4) Monogram negative can be used for variables: - $y ; # is equivalent to $y * -1
2. Integer comparison operator

Table 3.1. Integer comparison operator

Operator describe
< Less than
> Greater than
== equal
<= Less than or equal to
>= Greater than or equal to
!= Not equal to
<=> Compare, return 1, 0, or -1


The operator <=> result is:
0 - Two values ​​are equal
1 - The first one is large
1 - The second value is large
3. String comparison operator

Table 3.2. String comparison operator

Operator describe  
lt Less than  
gt Greater than  
eq equal  
le Less than or equal to  
ge Greater than or equal to  
ne Not equal to  
cmp Compare, return 1, 0, or -1  


4. Logical operators
Logical or: $a || $b or $a or $b
Logic and: $a && $b or $a and $b
Logical non:! $a or not $a
Logical XOR: $a xor $b
5. Bit operator
Bit &:&
bit or: |
Bit: ~
Position XOR:^
Move left: $x << 1
Move right: $x >> 2
Note: Do not use & for negative integers, because PERL will convert them into unsigned numbers.
6. Assignment operator

Table 3.3. Assignment operator

Operator describe
= Assignment only
+= Addition and assignment
-= Subtraction and assignment
*= Multiplication and assignment
/= Division and assignment
%= Remainder and assignment
**= Exponentiation and assignment
&= Bitwise AND and assignment
|= Bitwise OR and assignment
^= Bitwise XOR and assignment


Table 3.4. Assignment operator example

expression Equivalent expression
$a = 1; none (basic assignment)
$a -= 1; $a = $a - 1;
$a *= 2; $a = $a * 2;
$a /= 2; $a = $a / 2;
$a %= 2; $a = $a % 2;
$a **= 2; $a = $a ** 2;
$a &= 2; $a = $a & 2;
$a |= 2; $a = $a | 2;
$a ^= 2; $a = $a ^ 2;


.= can appear multiple times in an assignment statement, such as:
  $value1 = $value2 = "a string";
.= as a subexpression
  ($a = $b) += 3;
Equivalent to
  $a = $b;
  $a += 3;
But it is recommended not to use this method.
7. Self-increase and self-decrease operator:++, --(same usage as in C++)
.Don't use this operator on both sides of a variable: ++$var-- # error
.Do not use it again in the same expression after the variable increases/decrements: $var2 = $var1 + ++$var1; # error
.++ can be used in PERL for strings, but carry when the ending characters are 'z', 'Z', '9', such as:
  $stringvar = "abc";
  $stringvar++; # $stringvar contains "abd" now

  $stringvar = "aBC";
  $stringvar++; # $stringvar contains "aBD" now

  $stringvar = "abz";
  $stringvar++; # $stringvar now contains "aca"

  $stringvar = "AGZZZ";
  $stringvar++; # $stringvar now contains "AHAAA"

  $stringvar = "ab4";
  $stringvar++; # $stringvar now contains "ab5"

  $stringvar = "bc999";
  $stringvar++; # $stringvar now contains "bd000"
.Don't use --, PERL will convert the string to a number before subtracting it
  $stringvar = "abc";
  $stringvar--; # $stringvar = -1 now

.If the string contains non-letter and non-number characters, or the number is in the letter, the value before ++ operation is converted to the number zero, so the result is 1, such as:
  $stringvar = "ab*c";
  $stringvar++;
  $stringvar = "ab5c";
  $stringvar++;
8. String concatenation and repeat operators
Join: .
Repeat: x
Join and assign values ​​(similar to +=): .=
example:
  $newstring = "potato" . "head";
  $newstring = "t" x 5;
  $a = "be";
  $a .= "witched"; # $a is now "bewitched"
9. Comma operator
The previous expressions are first performed, such as:
  $var1 += 1, $var2 = $var1;
Equivalent to
  $var1 += 1;
  $var2 = $var1;
The only reason to use this operator is to improve the readability of the program and combine two closely related expressions, such as:
  $val = 26;
  $result = (++$val, $val + 5); # $result = 32
Note that if there are no brackets here, the meaning is different:
  $val = 26;
  $result = ++$val, $val + 5; # $result = 27
10. Conditional operator
Similar to C, the condition? value 1: value 2. When the condition is true, the value 1 is taken, and when it is false, the value 2 is taken, such as:
  $result = $var == 0 ? 14 : 7;
  $result = 43 + ($divisor == 0 ? 0 : $dividend / $divisor);
In PERL 5, you can also use the condition operator to select the assigned variable on the left side of the assignment, such as:
  $condvar == 43 ? $var1 : $var2 = 14;
  $condvar == 43 ? $var1 = 14 : $var2 = 14;
11. Order of operators

Table 3.6.Operator Order

Operator describe
++, -- Increase, decrease
-, ~, ! Monograph
** power
=~, !~ Pattern matching
*, /, %, x Multiply, divide, take the remainder, repeat
+, -, . Add, subtract, join
<<, >> Shift
-e, -r, etc. File Status
<, <=, >, >=, lt, le, gt, ge No waiting for comparison
==, !=, <=>, eq, ne, cmp Equal comparison
& bit and
|, ^ bit or bit or bit
&& Logic and
|| Logical or
.. List range
? and : Conditional operator
=, +=, -=, *=, Assignment
and so on  
, Comma operator
not Low-precedence logical NOT
and Low-precedence logical AND
or, xor Low-precedence logical OR and XOR

.Associativity:

Table 3.7. Operator binding

Operator Bonding
++, -- 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
and so on  
, Left-to-right
not Left-to-right
and Left-to-right
or, xor Left-to-right



suggestion:
1. When you are not sure whether an operator is executed first, you must use brackets to clarify it.
2. Use multiple lines, spaces, etc. to improve the readability of the program.