
# module
use Event;

# globals
my $c;

# functions
sub check {warn "[DBG] ", join('-', Event::_memory_counters), "\n";}

sub iosub
 {
  warn "[DBG] Entering IO watcher.\n"; check;
  # installing additional watchers
  for (my $l=0; $l<=5; $l++)
    {
     Event->io(
	       fh => \*STDIO,
	       timeout => "0.5$l",
	       repeat => 1,
	       cb => \&iosub,
	       prio => 5,
	       );
    }
  warn "[DBG] Installed additional IO watchers:\n"; check;
  # deinstall yourself
  $_[0]->w->cancel;
  warn "[DBG] Cancelled the initial IO watcher:\n"; check;
 }

# start now
warn "[DBG] Starting.\n"; check;

{
 Event->io(
	   fh => \*STDIO,
	   timeout => 0.5,
	   repeat => 1,
	   cb => \&iosub,
	   prio => 5,
	  );
}

Event->timer(
	     prio => 1,
	     interval => 2,
	     cb => sub {
                        warn "[DBG] Entering timer", '=' x 45, $c, '=' x 5, "\n"; check;
                        if (++$c==10)
			  {
			   my $cl;
			   warn "[DBG] Cleaning up.\n"; check;
			   print(STDERR "[DBG] Cleanup nr. ", $cl++, ": "), check, $_->cancel foreach Event::all_watchers;
			  }
                       }
	    );

warn "[DBG] Loop begins.\n"; check;
Event::loop();
warn "[DBG] All watchers were cancelled.\n"; check;
