It works without the seek. Each time some characters are picked up
they are printed. A new 'GOT THIS' message is only printed once a
new line is seen in the input (actually it should not be printed
until at least something has actually been received).
I find that it does... a slightly altered version with a couple of
pattern matching examples...
1) An specific error message is printed once a full line (i.e.
terminated by \n) is found to contain 'error'.
2) A critical message is printed immediately the incoming line is
found to contain 'critical' - there is no wait for the end of
line (the message is repeated as the line is built up until
terminated by \n).
#!/usr/bin/perl
use warnings;
use strict;
open (MYLOG, "/tmp/mylog") or die "Cannot open file: $!";
$|++;
my $line;
my $prefix = "GOT THIS: ";
print "Listening\n";
while ( 1 ) {
while (<MYLOG>) {
print $prefix, $_;
$prefix = '';
$line .= $_;
print "\nCritical *************\n" if $line =~ 'critical';
print "Error =============\n" if /\n/ && $line =~ 'error';
if (/\n/) {
$prefix = 'GOT THIS: ';
$line = '';
} else {
$prefix = '';
}
}
sleep (1);
}
__END__
Axel
Hey Axel,
I just wan't to mention some things about level 1 and 2 io.
Perl doesen't emulate platform IO (that I know of).
If you've ever used terminal emulation you have seen level 1 io,
where the characters are streamed real-time and local
processing might try to buffer, but if its not quick enough
characters are dropped. Thats level 1 io. Level 2 io is
a readline request waiting for the EOL character OR the EOF
condition on your filehandle. Thats what you are doing.
If the writer is doing level 1 io you will be getting EOF
condition most of the time. If writer is level 2 and does a
putline with no EOL you will get EOF, or if the writer
puts a string of escaped crlf's you will get them all
in a single readline. If the reader is too fast, the EOF
condition will always be set. If the reader is slowed
and gets an EOF I don't think the read position will
advance without seek(). So I would leave the seek in.
Btw, so whats wrong with "while ($line.=<MYLOG>)"
is that a Perl error?
gluck