Vigilando Ficheros

El Programa

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;

Ejecución

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



Subsecciones
Casiano Rodríguez León
Licencia de Creative Commons
Programación Distribuida y Mejora del Rendimiento
por Casiano Rodríguez León is licensed under a Creative Commons Reconocimiento 3.0 Unported License.

Permissions beyond the scope of this license may be available at http://campusvirtual.ull.es/ocw/course/view.php?id=44.
2012-06-19