$SIG{ALRM} = sub { die "timeout" }; eval { alarm(1200); # 20 minutes # long-time operations here alarm(0); }; if ($@) { if ($@ =~ /timeout/) { #timed out, do what you will here } else { die; # propagate the unexpected exception } }