Setting $| to non-zero enables autoflush on only the currently selected output file handle. By default this is STDOUT unless you have called select to change it
That means that, if you have opened a new handle to a file, $| will not affect its behaviour
Instead, you can use the IO::Handle module’s autoflush method. There is no need to use IO::Handle as IO::File, which subclasses IO::Handle, is loaded on demand by any version of perl since v5.14
It would look like this
open my $fh, '>', 'myfile.txt' or die $!;
$fh->autoflush;
After this, anything sent to the file using print $fh is immediately flushed to disk
solved Perl File Update After Each Print Statement [closed]