How can I write a SIG{__DIE__} handler that does not trigger in eval blocks?
Asked Answered
A

1

3

According to the perldoc -f die, which documents $SIG{__DIE__}

Although this feature was to be run only right before your program was to exit, this is not currently so: the $SIG{__DIE__} hook is currently called even inside evaled blocks/strings! If one wants the hook to do nothing in such situations, put die @_ if $^S; as the first line of the handler (see $^S in perlvar). Because this promotes strange action at a distance, this counterintuitive behavior may be fixed in a future release.

So let's take a basic signal handler which will trigger with eval { die 42 },

package Stupid::Insanity {
  BEGIN { $SIG{__DIE__} = sub { print STDERR "ERROR"; exit; }; }
}

We make this safe with

package Stupid::Insanity {
  BEGIN { $SIG{__DIE__} = sub { return if $^S; print STDERR "ERROR"; exit; }; }
}

Now this will NOT trigger with eval { die 42 }, but it will trigger when that same code is in a BEGIN {} block like

BEGIN { eval { die 42 } }

This may seem obscure but it's rather real-world as you can see it being used in this method here (where the require fails and it's caught by an eval), or in my case specifically here Net::DNS::Parameters. You may think you can catch the compiler phase too, like this,

BEGIN {
  $SIG{__DIE__} = sub {
    return if ${^GLOBAL_PHASE} eq 'START' || $^S;
    print STDERR "ERROR";
    exit;
  };
}

Which will work for the above case, but alas it will NOT work for a require of a document which has a BEGIN statement in it,

eval "BEGIN { eval { die 42 } }";

Is there anyway to solve this problem and write a $SIG{__DIE__} handler that doesn't interfere with eval?

Alodie answered 21/1, 2021 at 21:21 Comment(0)
A
5

Check caller(1)

Just a bit further down the rabbit hole with [caller(1)]->[3] eq '(eval)'

return if [caller(1)]->[3] eq '(eval)' || ${^GLOBAL_PHASE} eq 'START' || $^S;

Crawl entire call stack

You can crawl the entire stack and be sure you're NOT deeply in an eval with,

for ( my $i = 0; my @frame = caller($i); $i++ ) {
  return if $frame[3] eq '(eval)'
}

Yes, this is total insanity. Thanks to mst on irc.freenode.net/#perl for the pointer.

Alodie answered 21/1, 2021 at 21:40 Comment(4)
You asked and got an answer. Why is that insanity? This is Holy Grail of other languages: introspection. :-).Collum
Now, what would be insane would be to use that without a BIG comment explaining it. Remember: Always have pity on the poor slob that has to look at your code six months from now. Especially since it might be you. :-)Collum
@Collum my comment links to this question. ;)Alodie
There's module Eval::Util on CPAN, which has functions for telling you if you're inside an eval.Hereat

© 2022 - 2024 — McMap. All rights reserved.