10

这是我的 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"

我希望每一行都有警告。有什么特别之处undef,导致这种情况不会发生?01

4

1 回答 1

13

记录perldoc perldiag完整并附有理由:

0对于等于或的数值常量,不会发出此警告,1因为它们经常在语句中使用,例如

1 while sub_with_side_effects();

至于undef,它是一个即使在 void 上下文中也可以使用的函数。egundef($x)做一些类似于 - 但不同于 - 的事情$x = undef();。(您通常需要后者。)在 void 上下文中使用不带 args 可能会发出警告undef,但它需要专门的代码,而且根本不需要。

于 2013-07-31T22:07:52.863 回答