pp2@nereida:~/src/perl/Event$ cat -n watchfile.pl 1 use warnings; 2 use strict; 3 use Event; 4 use IO::File; 5 6 my ($c, $file) = (0, "tailtest.tmp"); 7 8 my $IN; 9 10 open($IN, "> $file"); 11 open($IN, $file); 12 my $oldsize = -s $IN; 13 14 Event->timer( 15 interval => 1, 16 cb => sub { 17 print "Writing new line ".++$c,"\n"; 18 19 my $OUT; 20 open($OUT, ">> $file"); 21 $OUT->autoflush(1); 22 print $OUT "$c\n"; 23 close($OUT); 24 } 25 ); 26 27 Event->io( 28 fd => $IN, 29 prio => 0, 30 cb => sub { 31 return if -s $IN == $oldsize; 32 33 print "New line detected: ",scalar(<$IN>),"\n"; 34 $oldsize = -s $IN; 35 } 36 ); 37 38 Event::loop;
pp2@nereida:~/src/perl/Event$ perl watchfile.pl Writing new line 1 New line detected: 1 Writing new line 2 New line detected: 2 Writing new line 3 New line detected: 3 Writing new line 4 New line detected: 4 ^C