K
kj
Recipe 16.21 of the Perl Cookbook (3rd Ed) makes absolutely no
sense to me. The authors propose a "nested-evals" strategy to
guard against a race condition:
eval {
local $SIG{ALRM} = sub { die 'timeout' };
alarm 10;
eval {
# long-runing op
}
alarm 0;
}
alarm 0; # race condition protection
die if $@ and !~ /timeout/;
The first thing that makes no sense is that no error within the
inner eval loop (including the error generated by the SIGALRM
handler) has any effect on the $@ being tested. The tested $@ will
be true only in the exceedingly unlikely event that something goes
wrong while executing any of the lines inside the outer eval block
that are not within the inner one (i.e. the assignment to $SIG{ALRM}
and two of the three to calls to alarm.) For all intents and
purposes, $@ is always undef.
The second thing I don't understand is the purpose of the inner
"alarm 0"; it seems redundant.
Any help elucidating these question would be much appreciated.
kj
sense to me. The authors propose a "nested-evals" strategy to
guard against a race condition:
eval {
local $SIG{ALRM} = sub { die 'timeout' };
alarm 10;
eval {
# long-runing op
}
alarm 0;
}
alarm 0; # race condition protection
die if $@ and !~ /timeout/;
The first thing that makes no sense is that no error within the
inner eval loop (including the error generated by the SIGALRM
handler) has any effect on the $@ being tested. The tested $@ will
be true only in the exceedingly unlikely event that something goes
wrong while executing any of the lines inside the outer eval block
that are not within the inner one (i.e. the assignment to $SIG{ALRM}
and two of the three to calls to alarm.) For all intents and
purposes, $@ is always undef.
The second thing I don't understand is the purpose of the inner
"alarm 0"; it seems redundant.
Any help elucidating these question would be much appreciated.
kj