Based on an
idiom, I have the following as a template to handle timeouts:
{
local $SIG{__DIE__};
local $@ = "";
eval {
local $SIG{ALRM} = sub { die("alarm\n") };
alarm(3);
chomp($input = <STDIN>); # potentially long operation
alarm(0);
1;
} or do {
die($@) unless $@ eq "alarm\n";
# timed out
warn("No answer for three seconds.\n");
$input = "";
}
}
Here, I only want to
do_something_more(...) when the alarm is up, but pass on every other
$@. So, my
if ($@) actually does a little more than your example. Then again, it's subtly different with
or do { ... } vs.
eval { ... }; if ....
Hope this helps as an example as to why
$@ needs to be mucked around :)
Update: Localized
$SIG{__DIE__}. Thanks,
jplindstrom!</c>