[Q] Rec 16.21: Timing out an op (nested evals)

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top