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: $@"; }
In reply to Re^2: Best way to timeout on a read
by pjotrik
in thread Best way to timeout on a read
by pileofrogs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |