http://qs1969.pair.com?node_id=57525

zeno has asked for the wisdom of the Perl Monks concerning the following question:

I have a program which loops indefinitely, and every 30 seconds checks for a condition and prints to a log file if the condition exists. But this distilled version of the code does not print out to the logfile:

use strict; use warnings; $|++; # make buffer 1 (error happens with or without this) open LOG,">foo.log" or die "can't open foo.log: $!"; while (1) { print LOG (localtime).": something\n"; sleep(30); # if I comment this, print works }

As noted, if I comment out the sleep(30) line, the code writes out to the logfile while the program is running.

I also noted that if if I modify the code so it closes the logfile, it does write to the logfile:

use strict; use warnings; $|++; # make buffer 1 (error happens with or without this) open LOG,">foo.log" or die "can't open foo.log: $!"; for (my $t=0;$t<3;$t++) { print LOG (localtime).": something\n"; sleep(30); } close LOG or die "can't close log: $!";

But it seems to me that the first example ought to work. Any ideas of why this happens? I'm using Perl 5.6, and this happens from both my Linux machine and from ActiveState Perl 5.60. BTW, I can think of other ways to do this, I'm mostly curious as to what causes it not to work. Thanks.

-zeno

Replies are listed 'Best First'.
(tye)Re: open, sleep, & print together cause an error
by tye (Sage) on Feb 10, 2001 at 04:12 UTC

    Well, yes, if you are open to sleeping "together" and it ends up in print, well, it can cause more than an "error"... (:

    Seriously though, $|++ only makes the currently selected output file handle non-buffering (STDOUT, by default). You can do the Perl4-ish thing (updated slightly to use my):

    { my $selected= select(LOG); $|++; # Unbuffer output to LOG select( $selected ); }

    Or you can do:

    require IO::Handle; LOG->autoflush(1);

    which accomplishes the same thing.

            - tye (but my friends call me "Tye")

      tye, you are a smart, smart person. That worked. Here's what I ended up with:

      use strict; use warnings; require IO::Handle; # <--- new open LOG,">foo.log" or die "can't open foo.log: $!"; <b>LOG->autoflush(1);</b> # <--- new while(1) { print LOG (localtime).": something\n"; sleep(5); }

      Thanks a lot for your help, tye. -zeno

Re: open, sleep, & print together cause an error
by arhuman (Vicar) on Feb 10, 2001 at 04:14 UTC

    did you try select on LOG before forcing autoflush ($|++;)?
    UPDATE : Forget this post, I took too much time to submit this one. And the question was answered during the looooong time I took to check my answer. :-(
      Hi arhuman, this code ended up working, too. Now I have a couple of alternatives!:
      use strict; use warnings; open LOG,">foo.log" or die "can't open foo.log: $!"; my $selected = select(LOG); $|++; select($selected); while(1) { print LOG (localtime).": something\n"; sleep(5); }

      Thanks a lot, arhuman. -zeno