Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-28 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found