perl中的运算符不能为两个代码提供相同的结果

时间:2014-09-18 22:16:51

标签: perl

我正在使用非运算符测试两个perl代码,但看起来它给出了两个不同的结果,即

$a = 1;

print "the value of a is $a\n";

$a = $a - 1;
$b = not $a;

print "value of b is $b\n";

当我运行上面的代码时,我得到了

the value of a is 1
value of b is 1

但是当我将上面的代码修改为以下

$a = 1;

print "the value of a is $a\n";

#$a = $a - 1;
$b = not $a--;

print "value of b is $b\n";

我得到以下结果

the value of a is 1
value of b is

在第一个代码中不应该not($a)与第二个代码中的not($a--)相同吗?

1 个答案:

答案 0 :(得分:3)

$a--表示递减$a,但在递减之前返回值。让代码与您想要的--$a相同。

有关详细信息,请参阅:perlop - Auto-increment and Auto-decrement