# Perl运算符/操作符用法完整指南

2021年4月2日12:32:34 发表评论 1,330 次浏览

#### 算术运算符

``````# Perl Program to illustrate the Airthmetic Operators

# Operands
\$a = 10;
\$b = 4;

# using airthmetic operators
print "Addition is: " , \$a + \$b , "\n" ;
print "Subtraction is: " , \$a - \$b , "\n" ;
print "Multiplication is: " , \$a * \$b , "\n" ;
print "Division is: " , \$a / \$b , "\n" ;
print "Modulus is: " , \$a % \$b , "\n" ;
print "Exponent is: " , \$a ** \$b , "\n" ;``````

``````Addition is: 14
Subtraction is: 6
Multiplication is: 40
Division is: 2.5
Modulus is: 2
Exponent is: 10000``````

#### 关系运算符

• 等于运算符：" =="检查两个值是否相等。如果等于则返回1, 否则不返回任何值。
• 不等于运算符："！="检查两个值是否相等。如果不相等, 则返回1, 否则不返回任何值。
• 等于运算符的比较：" <=>"如果左操作数小于右操作数, 则返回-1, 如果等于, 则返回0, 否则返回1。
• 大于运算符：">"如果left操作数大于right返回1, 否则不返回任何值。
• 少于运算符：" <"如果左操作数小于右操作数则返​​回1, 否则不返回任何值。
• 大于等于运算符："> ="如果左操作数大于或等于右, 则返回1, 否则不返回任何值。
• 小于等于运算符：" <="如果左操作数小于或等于右, 则返回1, 否则不返回任何值。

``````# Perl Program to illustrate the Relational Operators

# Operands
\$a = 10;
\$b = 60;

# using Relational Operators
if ( \$a == \$b )
{
print "Equal To Operator is True\n" ;
}
else
{
print "Equal To Operator is False\n" ;
}

if ( \$a != \$b )
{
print "Not Equal To Operator is True\n" ;
}
else
{
print "Not Equal To Operator is False\n" ;
}

if ( \$a > \$b )
{
print "Greater Than Operator is True\n" ;
}
else
{
print "Greater Than Operator is False\n" ;
}

if ( \$a < \$b )
{
print "Less Than Operator is True\n" ;
}
else
{
print "Less Than Operator is False\n" ;
}

if ( \$a >= \$b )
{
print "Greater Than Equal To Operator is True\n" ;
}
else
{
print "Greater Than Equal To Operator is False\n" ;
}

if ( \$a <= \$b )
{
print "Less Than Equal To Operator is True\n" ;
}
else
{
print "Less Than Equal To Operator is False\n" ;
}
if ( \$a <=> \$b )
{
print "Comparison of Operator is True\n" ;
}
else
{
print "Comparison of Operator is False\n" ;
}``````

``````Equal To Operator is False
Not Equal To Operator is True
Greater Than Operator is False
Less Than Operator is True
Greater Than Equal To Operator is False
Less Than Equal To Operator is True
Comparison of Operator is True``````

#### 逻辑运算符

• 逻辑与：‘和当同时考虑到两个条件时, ’运算符将返回true。例如, 当a和b都为真(即非零)时, \$a和\$b为真。你也可以使用&&.
• 逻辑或：‘or当满足其中一个(或两个)条件时, ’运算符将返回true。例如, 如果a或b中的一个为真(即非零), 则\$a或\$b为真。当然, 当a和b均为true时, 结果为" true"。你也可以使用||
• 逻辑非：如果满足考虑的条件, 则"非"运算符将给出1。例如, 如果\$d为0, 则not(\$d)为true。

``````# Perl Program to illustrate the Logical Operators

# Operands
\$a = true;
\$b = false;

# AND operator
\$result = \$a && \$b ;
print "AND Operator: " , \$result , "\n" ;

# OR operator
\$result = \$a || \$b ;
print "OR Operator: " , \$result , "\n" ;

# NOT operator
\$d = 0;
\$result = not( \$d );
print "NOT Operator: " , \$result ;``````

``````AND Operator: false
OR Operator: true
NOT Operator: 1``````

#### 按位运算符

• ＆(按位与)将两个数字作为操作数, 并对两个数字的每一位进行"与"运算。仅当两个位均为1时, AND的结果才为1。例如
```\$a = 13;    // 1101
\$b = 5;   //  0101
\$c = \$b & \$a;
print \$c;
Here Ouput will be 5

Explanation:

\$a = 1 1 0 1
\$b = 0 1 0 1
---------------
\$c = 0 1  0 1
---------------
```
• | (按位或)将两个数字作为操作数, 并对两个数字的每一位执行"或"运算。 OR的结果为1, 两个位中的任何一个为1。例如
```\$a = 13;    // 1101
\$b = 5;     // 0101
\$c = \$b | \$a;
print \$c;
Here Output will be 13

Explanation:
\$a = 1 1 0 1
\$b = 0 1 0 1
---------------
\$c = 1 1  0 1
---------------
```
• ^(按位XOR)将两个数字作为操作数, 并对两个数字的每一位进行XOR。如果两个位不同, 则XOR的结果为1。例如
```\$a = 13;  // 1101
\$b = 5;   // 0101
\$c = \$b ^ \$a;
print \$c;
Here Output will be 9

Explanation:
\$a = 1 1 0 1
\$b = 0 1 0 1
-------------
\$c = 1 0 0 1
-------------```
• 〜(补码运算符)一元运算符充当翻转位。它的工作是反转位并由于带符号的二进制数而使用2的补码形式重新使用。
• (<<)二进制左移运算符将使用两个数字, 左移第一个操作数的位, 第二个操作数决定移位的位数。它将左操作数乘以右操作数指定的次数。例如：
```\$a = 60;
\$c = \$a << 2;
print \$c;

Output: 240

Explanation:
60 * 2  =  120 ---(1)
120 * 2 = 240  ---(2)
```
• (>>)二进制右移运算符将取两个数字, 右移第一个操作数的位, 第二个操作数确定要移位的位数。它将左操作数除以右操作数指定的次数。例如：
```\$a = 60;
\$c = \$a >> 2;
print \$c;
Output: 15

Explanation:
60 / 2   = 30   ---(1)
30 / 2   =  15  ---(2)
```

``````# Perl Program to illustrate the Bitwise operators
#!/usr/local/bin/perl
use integer;

# Operands
\$a = 80;
\$b = 2;

# Bitwise AND Operator
\$result = \$a & \$b ;
print "Bitwise AND: " , \$result , "\n" ;

# Bitwise OR Operator
\$result = \$a | \$b ;
print "Bitwise OR: " , \$result , "\n" ;

# Bitwise XOR Operator
\$result = \$a ^ \$b ;
print "Bitwise XOR: " , \$result , "\n" ;

# Bitwise Complement Operator
\$result = ~ \$a ;
print "Bitwise Complement: " , \$result , "\n" ;

# Bitwise Left Shift Operator
\$result = \$a << \$b ;
print "Bitwise Left Shift: " , \$result , "\n" ;

# Bitwise Right Shift Operator
\$result = \$a >> \$b ;
print "Bitwise Right Shift: " , \$result , "\n" ;``````

``````Bitwise AND: 0
Bitwise OR: 82
Bitwise XOR: 82
Bitwise Complement: -81
Bitwise Left Shift: 320
Bitwise Right Shift: 20``````

#### 赋值运算符

" ="(简单赋值)：这是最简单的赋值运算符。该运算符用于将右侧的值分配给左侧的变量。

``````\$a = 10;
\$b = 20;``````

" + ="(添加操作符)

：此运算符是" +"和" ="运算符的组合。该运算符首先将左侧变量的当前值添加到右侧的值, 然后将结果分配给左侧的变量。

``(\$a += \$b) can be written as (\$a = \$a + \$b)``

"-="(减法操作符)

：此运算符是"-"和" ="运算符的组合。该运算符首先从右侧的值中减去左侧变量的当前值, 然后将结果分配给左侧的变量。

``(\$a -= \$b) can be written as (\$a = \$a - \$b)``

" * ="(乘法分配)

：此运算符是" *"和" ="运算符的组合。此运算符首先将左侧变量的当前值乘以右侧值, 然后将结果分配给左侧变量。

``(\$a *= \$b) can be written as (\$a = \$a * \$b)``

" / ="(部门分配)

：此运算符是" /"和" ="运算符的组合。该运算符首先将左侧变量的当前值除以右侧值, 然后将结果分配给左侧变量。

``(\$a /= \$b) can be written as (\$a = \$a / \$b)``

"%="(模量分配)

：此运算符是"%"和" ="运算符的组合。该运算符首先对右侧变量的当前值乘以右侧变量的值, 然后将结果赋给左侧变量。

``(\$a %= \$b) can be written as (\$a = \$a % \$b)``

" ** ="(指数分配)

：此运算符是" **"和" ="运算符的组合。该运算符首先用右边的值对左边变量的当前值进行指数运算, 然后将结果赋给左边的变量。

``(\$a **= \$b) can be written as (\$a = \$a ** \$b)``

``````# Perl program to demonstrate the working
# of Assignment Operators
#!/usr/local/bin/perl

# taking two variables & using
# simple assignments operation
\$a = 8;
\$b = 5;

# using Assignment Operators
print "Addition Assignment Operator: " , \$a += \$b , "\n" ;

\$a = 8;
\$b = 4;
print "Subtractation Assignment Operator: " , \$a -= \$b , "\n" ;

\$a = 8;
\$b = 4;
print "Multiplication Assignment Operator: " , \$a *= \$b , "\n" ;

\$a = 8;
\$b = 4;
print "Division Assignment Operator: " , \$a /= \$b , "\n" ;

\$a = 8;
\$b = 5;
print "Modulo Assignment Operator: " , \$a %= \$b , "\n" ;

\$a = 8;
\$b = 4;
print "Exponent Assignment Operator: " , \$a **= \$b , "\n" ;``````

``````Addition Assignment Operator: 13
Subtractation Assignment Operator: 4
Multiplication Assignment Operator: 32
Division Assignment Operator: 2
Modulo Assignment Operator: 3
Exponent Assignment Operator: 4096``````

#### 三元运算符

``condition ? first_expression : second_expression;``

``````condition: It must be evaluated to true or false.
If the condition is true
first_expression is evaluated and becomes the result.
If the condition is false, second_expression is evaluated and becomes the result.``````

``````# Perl program to demonstrate the working
# of Ternary Operator

\$x = 5;
\$y = 10;

# To find which value is greater
# Using Ternary Operator
\$result = \$x > \$y ? \$x : \$y ;

# displaying the output
print "The Larger Number is: \$result"``````

``The Larger Number is: 10``

``````# Perl program to demonstrate the working
# of Ternary Operator by using expression
# as the condition

# here maximum value can be 100
\$MAX_VALUE = 100;

# suppose user provide value
\$user_value = 444;

# To find which whether user provided
# value is staisfying the maximum value
# or not by using Ternary Operator
\$result = \$user_value <= \$MAX_VALUE ? \$user_value : \$MAX_VALUE ;

# displaying the output
# Here it will be MAX_VALUE
print "\$result"``````

``100``