How to detect if Perl code is being run inside an eval?

  • A+
Category:Languages

I've got a module that uses an @INC hook and tries to install missing modules as they are used. I don't want this behaviour to fire inside an eval. My attempt to do this currently is:

return     if ( ( $caller[3] && $caller[3] =~ m{eval} )     || ( $caller[1] && $caller[1] =~ m{eval} ) ); 

That's the result of me messing around with the call stack in some experiments, but it's not catching everything, like this code in HTTP::Tinyish:

sub configure_backend {     my($self, $backend) = @_;     unless (exists $configured{$backend}) {         $configured{$backend} =           eval { require_module($backend); $backend->configure };     }     $configured{$backend}; }  sub require_module {     local $_ = shift;     s!::!/!g;     require "$_.pm"; } 

Maybe I just need to traverse every level of the call stack until I hit an eval or run out of levels. Is there a better or easier way for me to figure out whether or not code is being wrapped in an eval without traversing the call stack?

 


If $^S is true, the code is inside an eval.

sub foo { print $^S } eval { foo() };  # 1 foo();           # 0 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: