Vigilando Variables

El Programa

pp2@nereida:~/src/perl/Event$ cat -n ./variablewatcherwriteonly.pl
 1  #!/usr/bin/perl
 2  use warnings;
 3  use strict;
 4
 5  use Event qw{time};
 6  require NetServer::Portal::Top;
 7
 8  my $var = 0;
 9
10  Event->timer(
11    interval => 2,
12    cb => sub {
13      $var++;
14      warn time.": Modified variable\n";
15      return;
16    }
17  );
18
19  Event->var(
20    var => \$var,
21    poll => 'r',
22    cb => sub { print time.": Read detected\n"; },
23  );
24
25  Event->var(
26    var => \$var,
27    poll => 'w',
28    cb => sub { print time.": Write detected. new value: $var\n" },
29  );
30
31  Event::loop;

Sólo es posible vigilar escalares. No es posible vigilar hashes o listas. Es posible detectar un acceso de lectura a un hash vigilando una referencia al hash (siempre que las lecturas y escrituras se hagan por medio de dicha referencia).

Ejecución

pp2@nereida:~/src/perl/Event$ variablewatcherwriteonly.pl
1209458901.6299: Modified variable
1209458901.63007: Write detected. new value: 1
1209458901.63017: Read detected
1209458903.62985: Modified variable
1209458903.62994: Read detected
1209458903.62998: Write detected. new value: 2
1209458903.63002: Read detected
1209458905.62981: Modified variable
1209458905.6299: Read detected
1209458905.62994: Write detected. new value: 3
1209458905.62998: Read detected
1209458907.62976: Modified variable
1209458907.62985: Read detected
1209458907.62989: Write detected. new value: 4
1209458907.62993: Read detected
1209458909.62972: Modified variable
1209458909.62981: Read detected
1209458909.62985: Write detected. new value: 5
1209458909.62989: Read detected
^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