ShaZe:

It looks like your program is monitoring a file that a different program generates. I suspect that the other program doesn't flush its buffers after writing, so the data in the file is simply incomplete at the time you read it. You can demonstrate it with these two programs. The first program writes a line of text to a file every second:

$ cat file_gen.pl #!/usr/bin/env perl use strict; use warnings; open my $FH, '>', 'a_file.txt'; $|=1; for (1 .. 1000) { print $FH "$_: here's 80 stars: ", "*"x80, "\n"; my $t = time; print "$t: line: $_\n"; sleep 1; }

The second program reads the file every 5 seconds, and dumps its contents to the console:

#!/usr/bin/env perl use strict; use warnings; while (1) { my $t = time; print "$t: Here's what we have so far:\n"; open my $FH, '<', 'a_file.txt'; my @lines = <$FH>; close $FH; print join("", @lines),"\n"; sleep 5; }

If you run the file generator in one terminal, and the file monitor in the other monitor, you'll see that many lines are written by the first program, while the monitor program doesn't see anything for a while. After a while, the first program will write enough data to the output buffer that the file finally gets updated, and the next time the file monitor does its thing, you'll see a large number of lines suddenly appear in the file. It's quite likely that the last line it shows you is a part of a line, though, unless you get lucky and the end of the line happens to correspond with the end of a buffer.

For details on buffering, read perldoc perlvar in the section "Variables related to filehandles" and also IO::Handle for information on file buffering as well as other operations you can do with files.

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Read / Write Server by roboticus
in thread Read / Write Server by ShaZe

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.