0

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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Gabs00
  • 1,869
  • 1
  • 13
  • 12

1 Answers1

3

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);
hobbs
  • 223,387
  • 19
  • 210
  • 288
  • thanks this worked (ancient version) ! Will accept in a couple of minutes. So $| only takes off buffering for stdout then? unless the new filehandle is selected? – Gabs00 Jan 28 '14 at 04:13
  • 1
    In ancient times, `use IO::Handle; $out->autoflush(1);` works too; in even more ancient times, s/IO::Handle/FileHandle/ – ysth Jan 28 '14 at 04:30
  • @ysth very true, but my memory isn't good enough to keep what works when straight, so I fall back to the 5.000 (or less) version :) – hobbs Jan 28 '14 at 04:31
  • `$|` is one of several variables that just affect the currently selected or last input filehandle – ysth Jan 28 '14 at 04:31