Skip to content

Latest commit

 

History

History
368 lines (258 loc) · 7.84 KB

12.md

File metadata and controls

368 lines (258 loc) · 7.84 KB

Perl 运算符 - 完整指南

原文: https://beginnersbook.com/2017/02/perl-operators-complete-guide/

运算符是表示动作的字符,例如+是表示加法的算术运算符。

perl 中的运算符分为以下类型:

1)基本算术运算符

2)赋值运算符

3)自增和自减运算符

4)逻辑运算符

5)比较运算符

6)按位运算符

7)引用和引用类运算符

1)基本算术运算符

基本算术运算符是:+, - , *, /, %, **

+ 用于加法:$x + $y

- 用于减法:$x - $y

* 用于乘法:$x * $y

/ 用于划分:$x / $y

%用于模数:$x % $y

**注:**它返回余数,例如10 % 5将返回 0

**用于指数:$x ** $y

x 到的 y 次幂

#!/usr/local/bin/perl
$x = -4;
$y = 2;

$result = $x + $y;
print '+ Operator 输出: ' . $result . "\n";

$result = $x - $y;
print '- Operator 输出: ' . $result . "\n";

$result = $x * $y;
print '* Operator 输出: ' . $result . "\n";

$result = $x / $y;
print '/ Operator 输出: ' . $result . "\n";

$result = $x % $y;
print '% Operator 输出: ' . $result. "\n";

$result = $x ** $y;
print '** Operator 输出: ' . $result . "\n";

输出:

+ Operator 输出: -2
- Operator 输出: -6
* Operator 输出: -8
/ Operator 输出: -2
% Operator 输出: 0
** Operator 输出: 16

2)赋值运算符

perl 中的赋值运算符是:=, +=, -=, *=, /=, %=, **=

  • $x = $y会将变量y的值赋给变量x
  • $x += $y 等于$x = $x + $y
  • $x -= $y等于$x = $x - $y
  • $x *= $y等于$x = $x * $y
  • $x /= $y等于$x = $x / $y
  • $x %= $y等于$x = $x % $y
  • $x **= $y等于$x = $x ** $y

例:

#!/usr/local/bin/perl

$x = 5;
$result = 10;

print "\$x= $x and \$result=$result\n";
$result = $x;
print '= Operator 输出: ' . $result . "\n";

print "\$x= $x and \$result=$result\n";
$result += $x;
print '+= Operator 输出: ' . $result . "\n";

print "\$x= $x and \$result=$result\n";
$result -= $x;
print '-= Operator 输出: ' . $result . "\n";

print "\$x= $x and \$result=$result\n";
$result *= $x;
print '*= Operator 输出: ' . $result . "\n";

print "\$x= $x and \$result=$result\n";
$result /= $x;
print '/= Operator 输出: ' . $result . "\n";

print "\$x= $x and \$result=$result\n";
$result %= $x;
print '%= Operator 输出: ' . $result . "\n";

#assigning different value to $result for this operator
$result =2;
print "\$x= $x and \$result=$result\n";
$result **= $x;
print '**= Operator 输出: ' . $result . "\n";

输出:

$x= 5 and $result=10
= Operator 输出: 5
$x= 5 and $result=5
+= Operator 输出: 10
$x= 5 and $result=10
-= Operator 输出: 5
$x= 5 and $result=5
*= Operator 输出: 25
$x= 5 and $result=25
/= Operator 输出: 5
$x= 5 and $result=5
%= Operator 输出: 0
$x= 5 and $result=2
**= Operator 输出: 32

3)自增和自减运算符

++--

$x++相当于$x = $x + 1;

$x--相当于$x = $x - 1;

示例:

#!/usr/local/bin/perl

$x =100;
$y =200;
$x++;
$y--;
print"Value of \$x++ is: $x\n";
print"Value of \$y-- is: $y\n";

输出:

Value of $x++ is: 101
Value of $y-- is: 199

4)逻辑运算符

逻辑运算符与二进制变量一起使用。它们主要用于条件语句和循环以评估条件。

perl 中的逻辑运算符是:&&||!

&&and相同。如果xy都为真,则$x && $y返回true,否则返回false

||or相同。如果xy都为假,则$x || $y将返回false,否则返回true

!not是相同的。!$x将返回x的反面,这意味着如果xfalse则为true,如果xtrue则返回false

示例

#!/usr/local/bin/perl

$x = true;
$y = false;

$result = ($x and $y);
print"\$x and \$y: $result\n";
$result = ($x && $y);
print"\$x && \$y: $result\n";

$result = ($x or $y);
print"\$x or \$y: $result\n";
$result = ($x || $y);
print"\$x || \$y: $result\n";

#point to note is that not operator works
#with 0 and 1 only.
$x=0;
$result = not($x);
print"not\$x: $result\n";
$result = !($x);
print"\!\$x: $result\n";

输出:

$x and $y: false
$x && $y: false
$x or $y: true
$x || $y: true
not$x: 1
!$x: 1

5)比较(关系)运算符

它们包括:==, eq, !=, ne, >, gt, <, lt, >=, ge, <=, le

  • 如果左侧和右侧是相等的,==eq返回true
  • 如果左侧不等于运算符的右侧,则!=ne返回true
  • 如果左侧大于右侧,>gt将返回true
  • 如果左侧小于右侧,<lt返回true
  • 如果左侧大于或等于右侧,>=ge返回true
  • 如果左侧小于或等于右侧,<=le返回true

示例

#!/usr/local/bin/perl

$x = 3;
$y = 6;

if( $x == $y ){
  print "\$x and \$y are equal\n";
}else{
  print "\$x and \$y are not equal\n";
}

if( $x != $y ){
  print "\$x and \$y are not equal\n";
}else{
  print "\$x and \$y are equal\n";
}

if( $x > $y ){
  print "\$x is greater than \$y\n";
}else{
  print "\$x is not greater than \$y\n";
}

if( $x >= $y ){
  print "\$x is greater than or equal to \$y\n";
}else{
  print "\$x is less than \$y\n";
}

if( $x < $y ){
  print "\$x is less than \$y\n";
}else{
  print "\$x is not less than \$y\n";
}

if( $x <= $y){
  print "\$x is less than or equal to \$y\n";
}else{
  print "\$x is greater than \$y\n";
}

输出:

$x and $y are not equal
$x and $y are not equal
$x is not greater than $y
$x is less than $y
$x is less than $y
$x is less than or equal to $y

6)按位运算符

有六个按位运算符:&, |, ^, ~, <<, >>

$x = 11; #00001011
$y = 22; #00010110

按位运算符执行逐位处理。

$x & $y比较xy的相应位,如果两个位相等则生成 1,否则返回 0。在我们的例子中它将返回:2,这是 00000010,因为只有xy的二进制形式倒数第二位是匹配的。

$x | $y比较xy的相应位,如果任一位为 1 则生成 1,否则返回 0。在我们的例子中它将返回 31,即 00011111

$x ^ $y比较xy的相应位,如果它们不相等则生成 1,否则返回 0。在我们的例子中它将返回 29,相当于 00011101

~$x是一个补码运算符,只是将位从 0 更改为 1,1 更改为 0。在我们的示例中,它将返回-12,其签名为 8 位,相当于 11110100

<<是左移位运算符,向左移动位,丢弃最左边的位,并将最右边的位赋值为 0。输出情况输出为 44,相当于 00101100

注意:在下面的示例中,我们在此移位运算符的右侧提供 2,这是位向左移动两个位置的原因。我们可以更改此数字,并且位将按运算符右侧指定的位数移动。同样适用于右侧运算符。

>>是右移位运算符,将位向右移动,丢弃最右位,并将最左边的位指定为 0。在我们的情况下输出为 2,相当于 00000010

示例:

#!/usr/local/bin/perl
use integer;

$x = 11; #00001011
$y = 22; #00010110

$result = $x & $y;
print "\$x & \$y: $result\n";

$result = $x | $y;
print "\$x | \$y: $result\n";

$result = $x ^ $y;
print "\$x ^ \$y: $result\n";

$result = ~$x;
print "~\$x = $result\n";

$result = $x << 2;
print "\$x << 2 = $result\n";

$result = $x >> 2;
print "\$x >> 2 = $result\n";

输出:

$x & $y: 2
$x | $y: 31
$x ^ $y: 29
~$x = -12
$x << 2 = 44
$x >> 2 = 2

7)引用和引用类操作符

perl 中的运算符有几个引用,如q{}, qq{}, qw{}, m{}, qr{}, s{}{}, tr{}{}, y{}{}。但是经常只使用其中两个:q{}用于单引号,qq{}用于双引号。

示例

q{Welcome to beginnersbook}会返回'Welcome to beginnersbook'

qq{Welcome to beginnersbook}会返回"Welcome to beginnersbook"