I don't understand why this buffering occurs in this case as normally it doesn't
I'd say it's the other way around: buffering is the default
behaviour. Only under certain circumstances, the behaviour is changed,
for example when the handle is connected to a terminal (like STDOUT,
typically). In this case, line-buffering (as opposed to block-buffering)
is used, because an interactive mode of operation is assumed.
You could enable auto-flushing, though (on any handle):
use IO::Handle;
open my $fh, ">", "test.out" or die $!;
$fh->autoflush();
print $fh "..."; # is being written immediately
(see also Re: Troubles with do...while loop and sleep (line- vs. block-buffered)) |