in reply to Re: Best way to timeout on a read
in thread Best way to timeout on a read

I second the recommendation to use exception instead of goto and to reset the alarm afterwards.

Personally, I prefer to have the signal handler and alarm calls enclosed inside the eval as well - to me, it's more obvious what's going on that way. alarm and perlipc show the same approach:

eval { local $SIG{ALRM} = sub { die "timeout" }; alarm 10; $input = <STDIN>; alarm 0; }; if ($@ and $@ =~ /timeout/) { print "Reading timed out"; } elsif ($@) { die "Other error: $@"; }