📜  Perl运算符

📅  最后修改于: 2021-01-07 08:14:25             🧑  作者: Mango

Perl运算子

perl运算符是用作语法的一系列符号。运算符是一种函数,其操作数是参数。

Perl运算符优先级

Perl优先级的行为类似于Math 中的BODMAS。加法和减法始终在乘法和除法之后。

例如,

8 + 4 - 5 * 6 / 3 = 2

在这里,答案将为BODMAS规则为2。将首先计算(6/3 = 2),然后将商2乘以5,然后进行减法和加法运算。

例:

use 5.010;
use strict;
use warnings;
my $result1 = 8 + 4 - 5 * 6 / 3 ;
say $result1;
my $result2 = 12 * 3 + 2 ** 2 << 1;
say $result2;

输出:

2
80

Perl运算符优先级表

Operators Description
++, — Auto-increment, Auto-decrement
-, ~, ! Operators having one operand
** Exponentiation
=~, !~ Pattern matching operators
*, /, %, x Multiplication, Divisor, Remainder, Repetition
+, -, . Addition, Subtraction, Concatenation
<<, >> Shifting operators
-e, -r File status operators
<, <=, >, >=, lt, le, gt, ge Inequality comparison operators
==, !=, <=>, eq, nq, cmp Equality comparison operators
& Bitwise AND
|, ^ Bitwise OR and XOR
&& Logical AND
|| Logical OR
. . List range operators
? and : Conditional operators
=, +=, -=, *= Assignment operators
, Comma operator
not low precedence logical NOT
and low precedence logical AND
or, xor low precedence logical OR and XOR

Perl操作员关联

运算符的关联性可帮助您决定从(从左到右)还是从(从右到左)评估方程式。

操作顺序非常重要。有时双方都是相同的,但有时会产生巨大差异。

例如,

7 + 4 + 2 = 13

这个问题的答案从左到右在任何顺序上都是相同的。

3 ∗∗ 2 ∗∗ 3

这个问题的答案是左起(9 ∗∗ 3)和右起(3 ∗∗ 8)。这两个答案有很大的不同。

例:

use 5.010;
use strict;
use warnings;
my $result = 3 ** 2 ** 3;
say $result;

输出:

6561

Perl关联表

Operators Description
++, — Order of direction is not applicable here
-, ~, ! Right-to-Left
** Right-to-Left
=~, !~ Left-to-Right
*, /, %, x Left-to-Right
+, -, . Left-to-Right
<<, >> Left-to-Right
-e, -r Order of direction is not applicable here
<, <=, >, >=, 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
, Left-to-Right
not Left-to-Right
and Left-to-Right
or, xor Left-to-Right

佩尔·阿里特

运算符的数量可以定义为对其进行运算的操作数的数量。

具有null运算符零运算操作,一元运算运算符的一个操作数进行操作,二元运算符的两个操作数操作和listary运算符的操作数的列表操作。

例如,

3 + 3 ? 2

算术运算运算符通常保持关联。在这里,(3 + 3)首先求值,然后转到第二个(-)运算符。

例:

use 5.010;
use strict;
use warnings;
my $result = ( 5 - 2 + 10 ) * 2;
say $result;

输出:

26

Perl固定性

运算符固定性可以定义为其相对于其操作数的位置。

例如,

  • 中缀运算符出现在其操作数之间。

    3 + 2

    此处,+运算符出现在操作数3和2之间

  • 前缀运算符出现在其操作数之前。

    ! $ a–3x

    这里, !和-运算符出现在操作数$ a和3。

  • Postfix运算符出现在其操作数之后。

    $ x ++

    在此,++运算符出现在操作数$ x之后。

  • Circumfix运算符围绕其操作数。例如哈希构造函数和引用运算符..

    (qq […])

  • 后缀运算符遵循某些操作数并围绕某些操作数。

    $ hash {$ a}