eval { local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required alarm($timeout); # # do stuff that might time out # alarm 0; }; if ($@) { die unless $@ eq "alarm\n"; # propagate unexpected errors # handle the timed out operation } else { # operation didn't time out, handle it's result }