in reply to Running code a certain amount of time

use alarm function and catch SIG{ALRM} signal

sub some_sub { eval { local $SIG{ALRM} = sub {die "died in SIG ALRM";}; alarm(1); my $i=0; $i++ while 1; alarm(0); }; if ($@) { if ($@ =~ /died in SIG ALRM/) { print "caught\n"; } else { print $@; } } }