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

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

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 = 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
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.


NOTE: In my address everything before the first period is backwards;
and the last period, and everything after it, should be discarded.

Site Timeline