【发布时间】:2011-04-16 18:16:47
【问题描述】:
|| 在 Perl 中是如何工作的?我想实现c风格的||操作。
@ARRAY=qw(one two THREE four);
$i=0;
if(($ARRAY[2] ne "three")||($ARRAY[2] ne "THREE")) #What's the problem with this
{
print ":::::$ARRAY[2]::::::\n";
}
while(($ARRAY[$i] ne "three")||($ARRAY[$i] ne "THREE")) #This goes to infinite loop
{
print "->$ARRAY[$i]\n";
$i=$i+1;
}
【问题讨论】:
-
如果您想了解某些东西的工作原理,请阅读文档:perldoc.perl.org/perlop.html#C-style-Logical-Or
-
您应该查看
perltidy脚本,这将使您的内容更易于阅读和维护。您还真的需要使用use strict; use warnings;。如果你不这样做,你会遇到数百个 perl 会告诉你的可以避免的问题。
标签: perl logical-operators short-circuiting