简体   繁体   English

你能在 Perl 中强制刷新输出吗?

[英]Can you force flush output in Perl?

I have the following two lines in Perl:我在 Perl 中有以下两行:

print "Warning: this will overwrite existing files.  Continue? [y/N]: \n";
my $input = <STDIN>;

The problem is that the print line does not get executed before the Perl script pauses for input.问题是在 Perl 脚本暂停输入之前,不会执行打印行。 That is, the Perl script just seems to stop indefinitely for no apparent reason.I'm guessing that the output is buffered somehow (which is why I put the \\n in, but that doesn't seem to help).也就是说,Perl 脚本似乎无缘无故地无限期停止。我猜输出是以某种方式缓冲的(这就是我放入 \\n 的原因,但这似乎没有帮助)。

By default, STDOUT is line-buffered (flushed by LF) when connected to a terminal, and block-buffered (flushed when buffer becomes full) when connected to something other than a terminal.默认情况下,STDOUT 在连接到终端时是行缓冲的(由 LF 刷新),当连接到终端以外的其他东西时是块缓冲(当缓冲区变满时刷新)。 Furthermore, <STDIN> flushes STDOUT when it's connected to a terminal.此外,当<STDIN>连接到终端时,它会刷新 STDOUT。

This means这意味着

  • STDOUT isn't connected to a terminal, STDOUT 未连接到终端,
  • you aren't printing to STDOUT, or你没有打印到标准输出,或者
  • STDOUT's been messed with. STDOUT 被搞砸了。

print prints to the currently select ed handle when no handle is provided, so the following will work no matter which of the above is true:当没有提供句柄时, 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;

There are several ways you can turn on autoflush:您可以通过多种方式启用自动刷新:

$|++;

at the beginning, or also with a BEGIN block:在开头,或者也有一个BEGIN块:

BEGIN{ $| = 1; }

However, it seems to be something unusual with your configuration, because usually a \\n at the end triggers the flushing (at least of the terminal).但是,您的配置似乎有些不寻常,因为通常末尾的\\n会触发刷新(至少是终端)。

To those who don't want to call flush() following every print like a baby-sitter thing, because it might be in a loop or something and you simply want your print to be unbuffered, then simply put this at the top portion of your perl script:对于那些不想像保姆一样在每次print调用flush()的人,因为它可能处于loop或其他状态,而您只是希望您的print无缓冲,然后只需将其放在顶部你的 perl 脚本:

STDOUT->autoflush(1);

Thereafter, no need to call flush() after print .此后,无需在print之后调用flush()

use IO::Handle;
STDOUT->flush();

Yes.是的。 I made a subroutine for this in my util.pl file, which is require d in all my Perl programs.我在我的 util.pl 文件中为此创建了一个子程序,在我的所有 Perl 程序中都require它。

###########################################################################
# In: File handle to flush.
# Out: blank if no error,, otherwise an error message. No error messages at this time.
# Usage: flushfile($ERRFILE);
# Write any file contents to disk without closing file. Use at debugger prompt
# or in program.
sub flushfile
{my($OUTFILE)=@_;
my $s='';

my $procname=(caller(0))[3]; # Get this subroutine's name.

my $old_fh = select($OUTFILE);
$| = 1;
select($old_fh);

return $s; # flushfile()
}

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

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