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). I'm fairly new to perl, so I would appreciate any advise on how to get around this issue.
By default, STDOUT is line-buffered (flushed by LF) when connected to a terminal, and block-buffered (flushed when buffer gets full) when connected to something other than a terminal. Furthermore,
<STDIN>
flushes STDOUT when it's connected to a terminal.This means
print
prints to the currentlyselect
ed handle when no handle is provided, so the following will work no matter which of the above is true:or
There are several ways you can turn on autoflush:
at the beginning, or also with a
BEGIN
block: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).