我在 Perl 中有以下两行:
print "Warning: this will overwrite existing files. Continue? [y/N]: \n";
my $input = <STDIN>;
问题是在 Perl 脚本暂停输入之前,打印行不会被执行。也就是说,Perl 脚本似乎无限期地停止,没有明显的原因。我猜测输出以某种方式被缓冲(这就是我放入 \n 的原因,但这似乎没有帮助)。
默认情况下,STDOUT 在连接到终端时是行缓冲的(由 LF 刷新),而在连接到终端以外的其他设备时是块缓冲的(当缓冲区变满时刷新)。此外,<STDIN>
当连接到终端时刷新 STDOUT。
这意味着
- STDOUT 未连接到终端,
- 您没有打印到 STDOUT,或者
- STDOUT 被搞乱了。
print
打印到当前select
当未提供句柄时,将使用 ed 句柄,因此无论以上哪一项为真,以下内容都将起作用:
# Execute after the print.
# Flush the currently selected handle.
# Needs "use IO::Handle;" in older versions of Perl.
select()->flush();
or
# Execute anytime before the <STDIN>.
# Causes the currently selected handle to be flushed immediately and after every print.
$| = 1;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)