#!./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();
