perl-modperl mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Doug MacEachern <do...@pobox.com>
Subject Re: %SIG localization partly resolved
Date Wed, 01 Mar 2000 05:37:37 GMT
On Wed, 2 Feb 2000, Stas Bekman wrote:

> I was asked a few times about %SIG localization and have stumbled upon
> this problem myself. Either it's a $SIG{PIPE}, $SIG{__WARN__},
> $SIG{__DIE__} or $SIG{CHLD} -- the most relevant sigs for mod_perl.
> 
> Here is the problem. Consider this code:
> 
> MyRun.pm:
> ---------
> package MyRun;
> 
> $SIG{__DIE__} = \&mydie;
> 
> sub dienow{
>   print "Content-type: text/plain\n\n";
>   print "before die\n";
>   die("I'm dying...");
>   print "after die\n";
> }
> 
> sub mydie{
>   my $why = shift;
>   print "Die was trapped: $why\n";
>   exit;
> }
> 1;
> 
> die.pl
> ------
> use MyRun;
> MyRun::dienow();
> 
> works OK, the __DIE__ sig gets trapped, no problem. But you don't want to
> write code like this since it affects the whole process!
> 
> So I add local before $SIG{__DIE__}:
> 
>   local $SIG{__DIE__} = \&mydie;
> 
> and my sighandler is ignored. Notice that if I move all the code into a
> single script, local does work. Actually not the code, but only the
> sighandler assignment.

this is not a bug.  you've declared 'local $SIG{...}' at the file-scope of
your .pm, it goes out of scope after the file is compiled/run, and
$SIG{__DIE__} is restored to whatever it was before.  that's the last Perl
will touch that line of code, it doesn't come back into scope just because
you call a subroutine in the .pm's namespace.  Perl's local() isn't smart
enough to do what you're expecting.


Mime
View raw message