http://qs321.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