Can you force flush output in Perl?

John picture John · Nov 19, 2015 · Viewed 21.2k times · Source

I have the following two lines in 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. 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).

Answer

ikegami picture ikegami · Nov 19, 2015

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. Furthermore, <STDIN> flushes STDOUT when it's connected to a terminal.

This means

  • STDOUT isn't connected to a terminal,
  • you aren't printing to STDOUT, or
  • STDOUT's been messed with.

print prints to the currently selected handle when no handle is provided, so the following will work no matter which of the above is true:

# 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;