Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

In reply to open, sleep, & print together cause an error by zeno

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others studying the Monastery: (6)
    As of 2014-10-21 00:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (94 votes), past polls