2011-08-04 57 views

回答

5

始终使用警告,尤其上一个-liners。

Perl有没有真的还是假的命名常量,没有警告或严格启用“裸词”(的东西,可能是一个常数或功能,但没有)被悄悄解释为字符串。所以,你在做if("true")if("false"),而且比其他所有字符串"""0"是真实的。

10

如果严格运行:

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 } 
8

您正在使用裸字truefalse。光秃秃的话是一件坏事。如果你试试这个:

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")的任何值被认为是“假”。这些值没有内置关键字。你可以只用01(或粘在use constant { true => 1, false => 0};如果你感到困扰。:)

+0

我移植4GL代码到Perl 5,并有常量帮助。我已经忘记了这些。谢谢。 – octopusgrabbus

相关问题