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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |