40
use strict;
my $var = NULL;
将提高Bareword "NULL" not allowed while "strict subs" in use
如何在严格编译指令下将变量设置为NULL?
use strict;
my $var = NULL;
将提高Bareword "NULL" not allowed while "strict subs" in use
如何在严格编译指令下将变量设置为NULL?
错误有Perl中没有NULL。但是,变量可以是undef
,这意味着它们没有设置值。
我这里还有一个如何能在Perl中得到一个未定义的变量的一些例子:
my $var; # variables are undefined by default
undef $var; # undef() undefines the value of a variable
$var = undef; # same, using an alternative syntax
要检查一个变量的definedness,使用defined()
,即
print "\$var is undefined\n" unless defined $var;
+1。和powerboy,在检查是否有定义时要小心。它是“如果(定义$ var)”NOT“if(not undef $ var)”“。后者将取消定义$ var。 – eruciform 2010-07-11 19:18:40
@eruciform - 好点!谢谢! – powerboy 2010-07-11 19:39:22
默认为未定义的+1。 – fengshaun 2010-07-11 23:58:25