sub count { print "$upper_no\n"; my $timer; # declare first so you # can cancel it in it's own callback $timer = $mw->repeat(1000,sub{ $no++; if ($no >= $upper_no){ $timer->cancel } }); }