Question

I was working on a script when I suddenly realized I couldn't write to a file.

I've been able to get it down to this bit of code:

use strict;
use warnings;

open(my $out, '>>', 'log.txt') or die "$!";
print $out "test";

while(1){
    sleep 1;
}

I tried taking off buffering by setting $| = 1.

I'm working on a program that runs and does something every 10 minutes, so I am using sleep to wait the 10 minutes.

Was it helpful?

Solution

Unless you set $| to 1 while $out was selected, you didn't do anything.

If your perl is non-ancient, write

$out->autoflush(1);

If your perl is ancient, write

my $prev = select($out);
$| = 1;
select($prev);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top