# First, the thing you want to timeout must be inside of an eval block: eval { # Second, localize the signal handling and set alarm to the desired timeout limit: $SIG{ALRM} = sub { die "timeout\n" }; alarm 10; # Third, add the lines of code you specifically want to timeout: do_something_here_that_can_timeout(); }; # Fourth: CANCEL the alarm immediately or you may have problems later! # This should be AFTER the eval block, not inside the eval block: alarm 0; # Fifth: Check for errors to see if you had a timeout or something else: if ($@ eq "timeout\n") { # Do whaever you need if a timeout happened handle_timeout(); } else { # You had an actual error and not a timeout, so handle it: handle_error($@); # or just: die $@ }