You need to select the file handle that needs to be flushed before using $|=1.
sub flush { my $h = select($_[0]); my $af=$|; $|=1; $|=$af; select($h); } sub autoflush { my $h = select($_[0]); $|=$_[1]; select($h); } # Flush immediately without turning auto-flush on: flush(*FILE); flush(\*FILE); flush($fh); # Flush immediately and turn auto-flush on: autoflush(*FILE, 1); autoflush(\*FILE, 1); autoflush($fh, 1); # Turn auto-flush off: autoflush(*FILE, 0); autoflush(\*FILE, 0); autoflush($fh, 0);
Alternatively, if your file handle is an IO::Handle (or subclass) object, IO::Handle already provides methods:
# Flush immediately without turning auto-flush on: $io->flush(); # Flush immediately and turn auto-flush on: $io->autoflush(1); # Turn auto-flush off: $io->autoflush(0);
In reply to Re: How to force Perl to write imediately on harddisk?
by ikegami
in thread How to force Perl to write imediately on harddisk?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |