{ local $SIG{ALRM} = sub { die "alarm timeout" }; alarm $timeout; # Here goes the code that can hang, #for example network connections die $@ if $@ && $@ !~ /alarm timeout/; }