Perl 简明教程

Perl - Operators

What is an Operator?

使用表达式4 + 5 等于 9 可以给出简单的答案。这里 4 和 5 称为运算符,+ 称为运算符。Perl 语言支持许多运算符类型,但以下是重要且最常用的运算符列表−

  1. Arithmetic Operators

  2. Equality Operators

  3. Logical Operators

  4. Assignment Operators

  5. Bitwise Operators

  6. Logical Operators

  7. Quote-like Operators

  8. Miscellaneous Operators

让我们逐个查看所有运算符。

Perl Arithmetic Operators

假设变量 $a 容纳 10,变量 $b 容纳 20,那么以下是 Perl 算术运算符−

Sr.No.

Operator & Description

1

+ ( Addition ) 在运算符两侧添加值 Example − $a + $b 将给出 30

2

- (Subtraction) 从左手操作数中减去右手操作数 Example − $a - $b 将给出 -10

3

* (乘法)*在运算符的两侧乘以值 *Example − $a * $b 将给出 200

4

/ (Division) 将左手操作数除以右手操作数 Example − $b / $a 将给出 2

5

% (Modulus) 将左手操作数除以右手操作数并返回余数 Example − $b % $a 将给出 0

6

* (指数)*对运算符执行指数(幂)计算 *Example − $a*$b 将给出 10 的 20 次幂

Perl Equality Operators

这些也称为关系运算符。假设变量 $a 容纳 10,变量 $b 容纳 20,接下来让我们检查以下数字相等运算符−

Sr.No.

Operator & Description

1

== (equal to) 检查两个操作数的值是否相等,如果相等,则条件变为真。 Example − ($a == $b) 为假。

2

!= (not equal to) 检查两个操作数的值是否相等,如果值不相等,则条件变为真。 Example − ($a != $b) 为真。

3

&lt;&#8658; 检查两个操作数的值是否相等,并根据左参数在数值上小于、等于还是大于右参数,返回 -1、0 或 1。 Example − ($a <⇒ $b) 返回 -1。

4

&gt; (greater than) 检查左手操作数的值是否大于右手操作数的值,如果大于,则条件变为真。 Example − ($a > $b) 为假。

5

&lt; (less than) 检查左操作数的值是否小于右操作数的值,如果是,则条件变为真。 Example − ($a < $b) 为真。

6

&gt;= (greater than or equal to) 检查左操作数的值是否大于或等于右操作数的值,如果是,则条件变为真。 Example − ($a >= $b) 为假。

7

&#8656; (less than or equal to) 检查左操作数的值是否小于或等于右操作数的值,如果是,则条件变为真。 Example − ($a ⇐ $b) 为真。

以下是一个相等运算符列表。假设变量 $a 的值为“abc”,变量 $b 的值为“xyz”,让我们来检查以下字符串相等运算符 −

Sr.No.

Operator & Description

1

lt 如果左参数在字符串上小于右参数,则返回真。 Example − ($a lt $b) 为真。

2

gt 如果左参数在字符串上大于右参数,则返回真。 Example − ($a gt $b) 为假。

3

le 如果左参数在字符串上小于或等于右参数,则返回真。 Example − ($a le $b) 为真。

4

ge 如果左参数在字符串上大于或等于右参数,则返回真。 Example − ($a ge $b) 为假。

5

eq 如果左参数在字符串上等于右参数,则返回真。 Example − ($a eq $b) 为假。

6

ne 如果左参数在字符串上不等于右参数,则返回真。 Example − ($a ne $b) 为真。

7

cmp 返回 -1、0 或 1,具体取决于左参数在字符串上分别小于、等于或大于右参数。 Example − ($a cmp $b) 为 -1。

Perl Assignment Operators

假设变量 $a 的值为 10,变量 $b 的值为 20,那么以下是 Perl 中可用的赋值运算符及其用法 −

Sr.No.

Operator & Description

1

= 简单的赋值运算符,将右侧操作数的值赋给左侧操作数 Example − $c = $a + $b 将 $a + $b 的值赋给 $c

2

+= 加 AND 赋值运算符,它将右操作数加到左操作数上,并将结果赋给左操作数 Example − $c += $a 等同于 $c = $c + $a

3

-= 减 AND 赋值运算符,它将右操作数从左操作数中减去,并将结果赋给左操作数 Example − $c -= $a 等同于 $c = $c - $a

4

*= 乘 AND 赋值运算符,它将右操作数与左操作数相乘,并将结果赋给左操作数 Example − $c *= $a 等同于 $c = $c * $a

5

/= 除 AND 赋值运算符,它将左操作数除以右操作数,并将结果赋给左操作数 Example − $c /= $a 等同于 $c = $c / $a

6

%= 模操作符以及赋值运算符,它对两个操作数求模,并将结果分配给左操作数 Example , 即:$c %= $a 等价于 $c = $c % a

7

= Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand Example − $c *= $a is equivalent to $c = $c * $a

Perl Bitwise Operators

位运算符用于位运算上并逐位执行运算。假设 $a = 60,且 $b = 13,现在以二进制格式它们将变为以下形式:

$a = 0011 1100

$b = 0000 1101

$a&$b = 0000 1100

$a|$b = 0011 1101

$a^$b = 0011 0001

~$a  = 1100 0011

There are following Bitwise operators supported by Perl language, assume if $a = 60; and $b = 13

link:../perl/bitwise_operators_example.html[Show Example]
[%autowidth]
|===

|Sr.No.|Operator & Description
|1|*&*
Binary AND Operator copies a bit to the result if it exists in both operands.
*Example* − ($a & $b) will give 12 which is 0000 1100

|2|*|*
Binary OR Operator copies a bit if it exists in eather operand.
*Example* − ($a | $b) will give 61 which is 0011 1101

|3|*^*
Binary XOR Operator copies the bit if it is set in one operand but not both.
*Example* − ($a ^ $b) will give 49 which is 0011 0001

|4|*~*
Binary Ones Complement Operator is unary and has the efect of 'flipping' bits.
*Example* − (~$a ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.

|5|*<<*
Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand.
*Example* − $a << 2 will give 240 which is 1111 0000

|6|*>>*
Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.
*Example* − $a >> 2 will give 15 which is 0000 1111

|===


=== Perl Logical Operators

There are following logical operators supported by Perl language. Assume variable $a holds true and variable $b holds false then −

link:../perl/logical_operators_example.html[Show Example]
[%autowidth]
|===

|Sr.No.|Operator & Description
|1|*and*
Called Logical AND operator. If both the operands are true then then condition becomes true.
*Example* − ($a and $b) is false.

|2|*&&*
C-style Logical AND operator copies a bit to the result if it exists in both operands.
*Example* − ($a && $b) is false.

|3|*or*
Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true.
*Example* − ($a or $b) is true.

|4|*||*
C-style Logical OR operator copies a bit if it exists in eather operand.
*Example* − ($a || $b) is true.

|5|*not*
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.
*Example* − not($a and $b) is true.

|===


=== Quote-like Operators

There are following Quote-like operators supported by Perl language. In the following table, a {} represents any pair of delimiters you choose.

link:../perl/quote_like_operators_example.html[Show Example]
[%autowidth]
|===

|Sr.No.|Operator & Description
|1|*q{ }*
Encloses a string with-in single quotes
*Example* − q{abcd} gives 'abcd'

|2|*qq{ }*
Encloses a string with-in double quotes
*Example* − qq{abcd} gives "abcd"

|3|*qx{ }*
Encloses a string with-in invert quotes
*Example* − qx{abcd} gives `abcd`

|===


=== Miscellaneous Operators

There are following miscellaneous operators supported by Perl language. Assume variable a holds 10 and variable b holds 20 then −

link:../perl/miscellaneous_operators_example.html[Show Example]
[%autowidth]
|===

|Sr.No.|Operator & Description
|1|*.*
Binary operator dot (.) concatenates two strings.
*Example* − If $a = "abc", $b = "def" then $a.$b will give "abcdef"

|2|*x*
The repetition operator x returns a string consisting of the left operand repeated the number of times specified by the right operand.
*Example* − ('-' x 3) will give ---.

|3|*..*
The range operator .. returns a list of values counting (up by ones) from the left value to the right value
*Example* − (2..5) will give (2, 3, 4, 5)

|4|*++*
Auto Increment operator increases integer value by one
*Example* − $a++ will give 11

|5|*--*
Auto Decrement operator decreases integer value by one
*Example* − $a-- will give 9

|6|*->*
The arrow operator is mostly used in dereferencing a method or variable from an object or a class name
*Example* − $obj->$a is an example to access variable $a from object $obj.

|===


=== Perl Operators Precedence

The following table lists all operators from highest precedence to lowest.

link:../perl/operators_precedence_example.html[Show Example]

[source]

left terms and list operators (leftward) left → nonassoc ++ — right ** right ! ~ \ and unary + and - left =~ !~ left * / % x left + - . left << >> nonassoc named unary operators nonassoc < > ⇐ >= lt gt le ge nonassoc == != <⇒ eq ne cmp ~~ left & left | ^ left && left || // nonassoc .. …​ right ?: right = += -= *= etc. left , ⇒ nonassoc list operators (rightward) right not left and left or xor