可能重复:
How do I use boolean variables in Perl?Perl中是否有内置的true/false布尔值?
[[email protected] ~]$ perl -e 'if(true){print 1}'
1
[[email protected] ~]$ perl -e 'if(false){print 1}'
1
我既惊讶和true
通过false
的if
...
可能重复:
How do I use boolean variables in Perl?Perl中是否有内置的true/false布尔值?
[[email protected] ~]$ perl -e 'if(true){print 1}'
1
[[email protected] ~]$ perl -e 'if(false){print 1}'
1
我既惊讶和true
通过false
的if
...
始终使用警告,尤其上一个-liners。
Perl有没有真的还是假的命名常量,没有警告或严格启用“裸词”(的东西,可能是一个常数或功能,但没有)被悄悄解释为字符串。所以,你在做if("true")
和if("false")
,而且比其他所有字符串""
或"0"
是真实的。
如果严格运行:
perl -Mstrict -e 'if(true) { print 1 }'
,你会得到的原因:
Bareword "true" not allowed while "strict subs" in use at -e line 1.
它被解释为字符串"true"
或"false"
这是总是如此。常量并不在Perl定义的,但你可以自己做:
use constant { true => 1, false => 0 };
if(false) { print 1 }
您正在使用裸字true
和false
。光秃秃的话是一件坏事。如果你试试这个:
use strict;
use warnings;
if (true){print 1}
你可能会得到这样的事情:
Bareword "true" not allowed while "strict subs" in use at - line 3.
Execution of - aborted due to compilation errors.
所定义的任何值并不像0被认为是“真实的”。任何未定义的值或看起来像0(如0
或"0"
)的任何值被认为是“假”。这些值没有内置关键字。你可以只用0
和1
(或粘在use constant { true => 1, false => 0};
如果你感到困扰。:)
我移植4GL代码到Perl 5,并有常量帮助。我已经忘记了这些。谢谢。 – octopusgrabbus
看看“人perlsyn”或http://perldoc.perl.org/perlsyn.html –