my $failed; eval { local $SIG{ALRM} = sub { die "TIMEOUT\n" }; # N.B. \n required eval { alarm($seconds); ... code to execute with timeout here (which may die) ... 1; } or $failed = 1; # outer eval catches alarm that might fire JUST before this alarm(0) alarm(0); # cancel alarm (if code ran fast) die "$@" if $failed; 1; } or $failed = 1; if ( $failed ) { if ( defined $@ and $@ eq "TIMEOUT\n" ) { ... } else { ... } # some other error }