SoFunction
Updated on 2025-04-14

Introduction to the use of perl operator and simple application

Perl 5 basic tutorial - operator

1. Arithmetic operators: +(add), -(subtraction), *(multiple), /(division), **(multiple power), %(take the remainder), -(monogram 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 the operation; the right side of the operator cannot be zero
(4) Monogram negative can be used for variables: - $y ; # is equivalent to $y * -1
(5) Corresponding to +=, -=, *=, /=, **=, %=

2. Integer comparison operator

Table 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, -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 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 with:&
bit or: |
No: ~
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. 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 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;


Note:
1. = can appear multiple times in an assignment statement, such as:
  $value1 = $value2 = "a string";
2. = as a subexpression
  ($a = $b) += 3;
Equivalent to
  $a = $b;
  $a += 3;

7. Self-increase and self-decrease operators: ++,--

.Don't use this operator on both sides of a variable: ++$var-- # error
.Don't 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

1. Join: .
2. Repeat: x
3. 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. Operator priority

Table 5. Operator priority

Operator describe
++-- Increase, decrease
-~! Monograph
** power
=~!~ Pattern matching
*/%x Multiply, divide, take the remainder, repeat
+-. Add, subtract, join
<<>> Shift
-e-r, etc. File Status
<<=>>=ltlegtge No waiting for comparison
==!=<=>eqnecmp 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
orxor Low-precedence logical OR and XOR


Operator binding:

Table 6. 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
<<=>>=ltlegtge Left-to-right
==!=<=>eqnecmp 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
orxor Left-to-right


Simple application of PERL operator

1. Assignment operator

symbol example illustrate
= $x=$y; Assign the value of $x to $y
+= $x+=$y; $x=$x+$y; add $x and assign it to $x
-= $x-=$y; $x=$x-$y; subtract $x and assign value to $x
*= $x*=$y; $x=$x*$y; multiply $x and assign value to $x
/= $x/=$y; $x=$x/$y; divide $x to $y power and assign it to $x
**= $x**=$y; $x=$x* *$y; find $x times $y to power and assign it to $x
%= $x%=$y; $x=$x%$y; divide $x by the remainder of $y and then assign it to $x
.= $str1.=$str2; $str1=$str1.$str2; add the $str1 string and the $str2 string and then assign it to the $str character
string
x= $str x=$y; $str=$str x $y; loop $str character $y and then assign the result to
$str The string

2. Arithmetic operator

symbol example illustrate
+ $a=$x+$y Add $x and $y and assign value to $a
- $a=$x+$y Subtract $x and $y and then assign value to $a
* $a=$x*$y Multiply $x and $y and assign value to $a
/ $a=$x/$y Divide $x and $y and assign value to $a
% $a=$x%$y Divide $x and $y and then assign the remainder to $a
** $a=$x**$y Multiply $x and $y to the power of $y and then assign value to $a
++ $x++;++$x Add $x one and then assign it to $x
-- $x--;--$x Subtract $x one and then assign value to $x
. $a=$x.$y Combine the $x string and the $y string and then assign the value to $a

3. Number Values ​​operator

symbol example illustrate
> $x>$y If $x is greater than $y, return 1, otherwise return 0
>= $x>=$y If $x is greater than or equal to $y, return 1, otherwise return 0
< $x<$y If $x is less than $y, return 1, otherwise return 0
<= $x<=$y If $x is less than or equal to $y, return 1, otherwise return 0
== $x==$y If $x equals $y returns 1, otherwise return 0
!= $x!=$y If $x is not equal to $y, return 1, otherwise return 0
<=> $x<=>$y If $x is greater than $y, return 1; if $x is equal to $y, return 0;
If $x is less than $y, return -1

4. String Values ​​operator

symbol example illustrate
gt $str1 gt $str2 If $str1 is greater than $str2, return 1, otherwise return 0
ge $str1 ge $str2 If $str1 is greater than or equal to $str2, return 1, otherwise return 0
lt $str1 lt $str2 If $str1 is less than $str2, return 1, otherwise return 0
le $str1 le $str2 If $str1 is less than or equal to $str2, return 1, otherwise return 0
eq $str1 eq $str2 If $str1 is equal to $str2, return 1, otherwise return 0
ne $str1 ne $str2 If $str1 is not equal to $str2, return 1, otherwise return 0
cmp $str1 cmp $str2 If $str1 is greater than $str2, return 1; if $str1=$str2 returns 0;
If $str1 is less than $str2, return -1

5. Logical operators
1.$x && $y(AND)

$x $y result
True True True
True False False
False True False
False False False

1.$x || $y(OR)

$x $y result
True True True
True False True
False True True
False False False

1.! $x(Not)

$x result
True False
False True

6. Other operators

instruction: ..Range Operator
illustrate: This operator is a particularly useful operator in Perl language and is very practical.
example:
@digits=(1..9); #At this time @digits=(1,2,3,4,5,6,7,8,9);
@digits=('01'..'05'); #At this time @digits=(01,02,03,04,05);
@char=('A'..'E'); #At this time @char=(A,B,C,D,E);
@total=(1..3,'A'..'B'); #At this time @totalr=(1,2,3'A','B');

 

instruction: Judgment equation? Operation 1: Operation 2 conditional operator
illustrate: The meaning of this operator is the same as in C language. If you judge that the value of the operation is true
If the calculation formula is false, then the calculation formula is done.
Operation of 2.
example:
$price=($age>60)?100:200;
If $age is greater than 60, then $price is equal to 100; otherwise $price is equal to 200.

7. Commonly used file data (File test) operator

example illustrate
-r $file If $file is readable, return a value of 1
-w $file If $file is writeable, return a value of 1
-x $file If $file is executable, return a value of 1
-e $file If $file exists, return a value of 1
-o $file If $file is owned by the executor, return a value of 1
-s $file Returns the file size of $file (bytes)
-f $file If $file is a normal file, return a value of 1
-t $file If $file is a text file, return a value of 1
-b $file If $file is a binary file, return a value of 1
-m $file Returns the number of dates from the last time the file has changed to the present