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).
If you want to flush the buffered output at another time, call fflush , which is declared in the header file stdio. h . Preliminary: | MT-Safe | AS-Unsafe corrupt | AC-Unsafe lock corrupt | See POSIX Safety Concepts. This function causes any buffered output on stream to be delivered to the file.
$| = 1; forces a flush after every write or print, so the output appears as soon as it's generated rather than being buffered. See the perlvar documentation. $| is the name of a special variable.
print() operator – print operator in Perl is used to print the values of the expressions in a List passed to it as an argument. Print operator prints whatever is passed to it as an argument whether it be a string, a number, a variable or anything. Double-quotes(“”) is used as a delimiter to this operator.
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
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:
# 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{ $| = 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With