var watcher behaviour

[email protected] (Jochen Stenzel)
Newsgroups perl.loop
Message-ID <[email protected]>
Hello,

here comes another script. It installs a number of var watchers and then
modifies the variables. As you can see in comments, this was intended to
become another grouping test script, but the error has to do with var
watchers: whenever I modify a certain variable, the associated watcher is
called MULTIPLY.

Greetings

                    Jochen
multiple.var.callbacks.pl (application/x-perl, 1.2 KB)
#!./perl -w

use strict;

use Event qw(time);

# require Event::group;


# declare number of var watchers to install
use constant 'MAX' => 4;

# declare globals
my ($counter)=(0);

# prepare random generator
srand;

# prepare group watcher
# my $groupWatcher=Event->group(parked=>1, timeout => 2, cb => sub {my $e = shift; warn "All vars are still unchanged.\n";});

# install several var watchers and made them watched by the group watcher
foreach (1..MAX)
 {
  no strict 'refs';
  my $msg="You changed var $_.\n";
#   $groupWatcher->add(Event->var(var=>\${"var$_"}, cb=>sub {warn ++$counter, ". call: $msg";}, poll=>'rw'));
  Event->var(var=>\${"var$_"}, cb=>sub {warn ++$counter, ". call, watcher", $_[0]->w, ": $msg";}, poll=>'rw');
 }

# activate group watcher
# $groupWatcher->start;

# install a timer that modifies variables randomly
Event->timer(interval=>1, repeat=>1, cb=>sub {
                                              my $nr=int(rand(MAX))+1;
					      no strict 'refs';
	                                      warn "---> CHANGED var $nr (new value is ", ++${"var$nr"}, ").\n"; 
	                                      $_[0]->w->cancel if ${"var$nr"}>5;
                                             }
            );

# start loop
Event::loop();
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.