Perl sigdie handler and eval
I am overriding my SIG gradient handler as shown below in my Logger module.
# Catch die messages and log them with logdie
$SIG{__DIE__} = \&logdie;
Now below the program start as expected and post-processing will be called.
use strict;
use warnings;
use File::Path;
# use MyLogger;
my $dir="/random";
eval {
# local $SIG{__DIE__};
File::Path::make_path($dir);
};
if($@) {
warn("Cannot create $dir :$@ \n");
}
print "Post processing \n";
However, if I enable my log module and add use MyLogger
, the code crashes inside eval
statment with below error and post processing is not called.
[ERROR] 2015/04/27 22:19:07 Carp.pm:166> mkdir / random: permission denied on line. /test.pl 11.
One option to fix this is to add a local sigdie (as shown in the commented code).
However, my log module is used by many scripts.
Is there a way to change my Logger module so that it raises an ERROR message when called from within an eval block?
source to share
$^S
indicates if the current execution point is inside a block eval
:
$^S State
--------- -------------------------------------
undef Parsing module, eval, or main program
true (1) Executing an eval
false (0) Otherwise
So it looks like you want to check if $^S
at the beginning of your handler is true __DIE__
:
package MyLogger;
sub logdie {
$^S && die $_[0]; # use default die handler
... # else use custom die handler
}
source to share