in reply to Reliable Timeout

You have a few errors in the code you're trying.
  1. You need to wrap the code that might time out in an eval.
  2. The die $@ if $@ && $@ !~ /alarm timeout/; has to come after the eval.
  3. It's recommended, when throwing your own exceptions, to end the string in a newline, e.g. die "timeout\n".

# example { # this scope is here just to localize the signal handler local $SIG{ALRM} = sub { die "timeout\n" }; alarm $TIMEOUT; eval { # iffy code 1; } or do { die $@ unless $@ eq "timeout\n"; # propagate other exceptions # handle timeout }; alarm 0; # free alarm }

Replies are listed 'Best First'.
Re^2: Reliable Timeout
by lidden (Curate) on Feb 14, 2005 at 20:59 UTC
    Actually it is better to not add the newline in a die because if you dont Perl will add the line you died on and a newline.
      Which you don't generally need if you're expecting to catch your own exceptions :)

      If there's a chance the die won't be caught by my own code, I like to use Carp::confess instead of die anyway to get the stack trace.