简体   繁体   English

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

[英]What is the difference between <stdin> and <STDIN>?

When I use <stdin> in Perl module ( *.pm ) files it's not reading input from the keyboard, but when I use <STDIN> in the same place it works fine. 当我在Perl模块( *.pm )文件中使用<stdin> ,它不是从键盘读取输入,但是当我在同一个地方使用<STDIN>时它工作正常。

Why is it not getting input when I use <stdin> ? 当我使用<stdin>时为什么不输入?

STDIN is the documented filehandle. STDIN是文档化的文件句柄。 There exists stdin as well, which is aliased to STDIN , but it only works in the main:: package: main::stdin is the same as main::STDIN (as documented in perlop - Perl operators and precedence ). 还存在stdin ,它是STDIN别名,但它只能在main:: package中工作: main::stdinmain::STDIN相同(如perlop中所述 - Perl运算符和优先级 )。

In a package, therefore, 因此,在一个包中,

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

stdin is interpreted as My::Package::stdin , which doesn't exist. stdin被解释为My::Package::stdin ,它不存在。 You can use main::stdin from a package, but using the standard STDIN (which always points to main::STDIN , even from a package) is much cleaner. 你可以使用包中的main::stdin ,但是使用标准的STDIN (它总是指向main::STDIN ,甚至来自包)也更清晰。

Didn't know about this, but found it documented in a throw-away paragraph in perlop 不知道这一点,但发现它在perlop一个perlop段落中perlop

The filehandles STDIN , STDOUT , and STDERR are predefined. 文件句柄STDINSTDOUTSTDERR是预定义的。 (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. (文件句柄stdinstdoutstderr也可以在包中工作,它们将被解释为本地标识符而不是全局。)可以使用open()函数创建其他文件句柄。 See perlopentut and "open" in perlfunc for details on this. 有关详细信息,请参阅perlfunc中的perlopentut和“open”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM