为什么Perl抱怨"在void上下文中无用的常量",但有时只是?

qnt*_*ntm 10 perl constants void

这是我的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"
Run Code Online (Sandbox Code Playgroud)

我希望每一行都有警告.有什么特别之处undef,01这将导致此不发生?

tjd*_*tjd 13

记录perldoc perldiag完整的理由:

对于等于01因为它们经常在语句中使用的数值常量,将不会发出此警告

1 while sub_with_side_effects();
Run Code Online (Sandbox Code Playgroud)

至于undef,它是一个甚至在void上下文中使用的函数.例如undef($x)做类似的事情 - 但不同于 - $x = undef();.(您通常需要后者.)可以undef在void上下文中使用不带args的警告,但它需要专门的代码,而根本不需要它.