2013-07-31 19 views
10

这里是我的Perl脚本,其输出:为什么Perl会抱怨“在无效的情况下无用的常量”,但有时候呢?

use strict; 
use warnings; 

(undef, 1); # no output 
(0, 1);  # no output 
(1, 1);  # no output 
(2, 1);  # "Useless use of a constant in void context at C:\...\void.pl line 7" 
(3, 1);  # "Useless use of a constant in void context at C:\...\void.pl line 8" 
("", 1); # "Useless use of a constant in void context at C:\...\void.pl line 9" 
("0", 1); # "Useless use of a constant in void context at C:\...\void.pl line 10" 
("1", 1); # "Useless use of a constant in void context at C:\...\void.pl line 11" 

我希望在每行警告。 undef01什么是特殊的,导致这种情况不会发生?

回答

13

perldoc perldiag完整的文献记载理由:

此警告不会发出等于01数字常量,因为它们是在陈述经常使用像

1 while sub_with_side_effects(); 

至于undef,这是一个功能,甚至在无效的情况下使用。例如undef($x)与—类似,但不同于— $x = undef();。 (您通常需要后者。)在无效的情况下,可以针对undef没有参数的使用发出警告,但它需要专门的代码,并且根本不需要。

+0

我只是想说这个。它在无效使用%s的情况下在无效环境下表示:“此警告不会针对等于0或1的数字常量发出”http://perldoc.perl.org/perldiag.html#Useless-use-of- %s-in-void-context – chilemagic

+0

很好的挖掘文档。虽然没有解释'(undef,1)'。 (扔掉'undef'被认为没有丢弃任何东西?) –

+0

@Ted Hopp,我添加了对'undef'的解释。 – ikegami

相关问题