<stdin> 和 <STDIN> 有什么区别?

What is the difference between <stdin> and <STDIN>?

当我在 Perl 模块 (*.pm) 文件中使用 <stdin> 时,它不会从键盘读取输入,但是当我在同一个地方使用 <STDIN> 时,它工作正常。

为什么我使用 <stdin> 时没有输入?

STDIN 是记录的文件句柄。也存在 stdin,它是 STDIN 的别名,但它只适用于 main:: 包:main::stdinmain::STDIN 相同(如文档所述在 perlop - Perl operators and precedence).

因此,在一个包中,

package My::Package;
sub xx {
    print while <stdin>;
}

stdin 被解释为 My::Package::stdin,它不存在。您可以使用包中的 main::stdin,但使用标准的 STDIN(始终指向 main::STDIN,即使是包中的)也更简洁。

对此一无所知,但发现它记录在 perlop

的一个废弃段落中

The filehandles STDIN, STDOUT, and STDERR are predefined. (The filehandles stdin, stdout, and stderr will also work except in packages, where they would be interpreted as local identifiers rather than global.) Additional filehandles may be created with the open() function, amongst others. See perlopentut and "open" in perlfunc for details on this.