Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么Perl认为'1和0'是正确的?_Perl_Boolean - Fatal编程技术网

为什么Perl认为'1和0'是正确的?

为什么Perl认为'1和0'是正确的?,perl,boolean,Perl,Boolean,这是样品。出于某种奇怪的原因,Perl认为1和0是一个真值。为什么? $ perl -e '$x = 1 and 0; print $x;' 1 在您的示例中,运算符优先级为 perl -e '($x = 1) and 0; print $x;' 而你想要的是: perl -e '$x = (1 and 0); print $x;' 或 因为和以及&的优先级不同: $x=1和0类似于($x=1)和0,而$x=1和&0类似于$x=(1和&0) 请参阅。它不会: $ perl -e '$x =

这是样品。出于某种奇怪的原因,Perl认为
1和0
是一个真值。为什么?

$ perl -e '$x = 1 and 0; print $x;'
1

在您的示例中,运算符优先级为

perl -e '($x = 1) and 0; print $x;'
而你想要的是:

perl -e '$x = (1 and 0); print $x;'


因为
以及
&
的优先级不同:

$x=1和0
类似于
($x=1)和0
,而
$x=1和&0
类似于
$x=(1和&0)

请参阅。

它不会:

$ perl -e '$x = (1 and 0); print $x;'
0

特别是赋值运算符(
=
)的优先级低于
&&
,但高于
。请注意,它确实给出了警告。不幸的是,它没有给出“在无效上下文中无用地使用…”错误,因为该警告在
0时被抑制(和
1;
)。它只在作为
perl-we…
运行时发出警告(警告是
Found=in conditional,应该是==at-e第1行。
)。在我的例子
perl-e…
中,我没有得到任何警告。你的观点是什么?您应该始终使用
使用strict;使用警告。如果你不知道发生了什么事,那就更糟了。更不用说检查错误了。(这里不适用,但很多时候人们甚至不检查错误就问出了什么问题!)我的观点是你的短语“it doesgive a warning”是错误的。我说的是代码,不是你的命令。正确的回答应该是认识到你犯了一个愚蠢的错误,没有寻找错误,而不是开始一个毫无意义的线程,不管它是否给出警告。@bessarabov如果这个答案对你有效,你可以接受它。
$ perl -e '$x = (1 and 0); print $x;'
0