#!/usr/bin/perl use strict; use warnings; my @result; $SIG{ALRM} = sub { print "ALARM fired\n"; die "timeout"; }; eval { alarm(3); @result = `sleep 5`; alarm(0); }; print "Running after alarm: $@\n"; if ($@) { if ($@ =~ /timeout/i) { push @result, "App has hanged itself\n"; } else { alarm(0); # clear the still-pending alarm die; # propagate unexpected exception } } print "result is: ", join('', @result), "\n";