graq has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to build a module that will handle all our perl in a nice way, along the way I have trapped $SIG{__DIE__}. After doing my thing, I want to hand it back, but I get an "Use of uninitialized value in scalar assignment at Hps.pm" error.

The Package:

package Hps; my $origHandler = $SIG{__DIE__}; $SIG{__DIE__} = 'Hps::grimReaper' unless ($^D); sub grimReaper { # We can drop out early if $^S is set because that means we're in +an eval die (@_) if (not defined $^S or ($] >= 5.005 and defined $^S and $^S)); # .. do some stuff .. # It is this line that causes an error. $SIG{__DIE__} = $origHandler; die "@_"; }
The script:
#!/usr/bin/perl -w use strict; use Hps; die ("Oh no!");
Is this a feature of the -w switch, or can I do this in a nicer way?

Replies are listed 'Best First'.
Re: Handing back $SIG{__DIE__} under -w causes a warning?
by dave_the_m (Monsignor) on Jun 04, 2004 at 14:57 UTC
    You can get this with the simple program like the following:
    perl -we '$SIG{__DIE__} = undef'
    Basically you aren't allowed to assign undef values to %SIG slots. Instead you need something like:
    $SIG{__DIE__} = defined $origHandler ? origHandler : 'DEFAULT'
    Although arguably Perl ought to be smart enough to DWIM. I might fix this.

    Dave.

Re: Handing back $SIG{__DIE__} under -w causes a warning?
by Abigail-II (Bishop) on Jun 04, 2004 at 15:01 UTC
    Next time, please indicate which line is giving the warning - that helps the people willing to help you.

    Having said that, I've no idea why this is giving a warning. It's actually the $SIG {__DIE__} that's giving the problems - setting it to something that's undefined seems to cause the warning. Smells like a bug to me.

    Abigail