-5

When I write a file in Perl, the complete content of each Print statement is getting updated on the file once the program is complete.

Is there a way in which the file can be updated after each print statement is executed ?

I tried setting $|=1, but it does not seem to work.

ikegami
  • 367,544
  • 15
  • 269
  • 518
Sujeet Padhi
  • 254
  • 1
  • 20
  • I don't understand the problem. Can you show the code that suffers and explain more clearly what is not updated? (How do you even know how often a file is updated?) – zdim Jan 24 '18 at 20:25

1 Answers1

1

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

Borodin
  • 126,100
  • 9
  • 70
  • 144