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

Hi Monks, I would like to replace my die statement with a report dump (like a function call). Is there any way to pass the die message into the function call that would be trigger by the signal?

Replies are listed 'Best First'.
Re: Signals and die message
by mifflin (Curate) on Feb 24, 2005 at 15:44 UTC
    Try this:
    $SIG{__DIE__} = sub { # your handler here, message in $_[0] }
      And note that the error message you gave die() is passed in as the first argument to the sub. (see perldoc perlvar)

      Caution: Contents may have been coded under pressure.
      anyway to avoid seeing the message statement get outputed twice?
        Exit during the signal handler:
        $SIG{__DIE__} = sub { my $msg = shift; # This calls the real die die "What I really want to see"; }; die "Hide me!\n";

        Caution: Contents may have been coded under pressure.
        If you are seeing it twice you are probably printing the error message in your handler. If you don't wrap your die in an eval you will see the message twice...
        erickn@isfe:/home/erickn> cat y $SIG{__DIE__} = sub { print $_[0] }; die "hi"; erickn@isfe:/home/erickn> perl y hi at y line 4. hi at y line 4.
        Wrapping in an eval will stop it...
        erickn@isfe:/home/erickn> cat x $SIG{__DIE__} = sub { print $_[0] }; eval { die "hi"; }; erickn@isfe:/home/erickn> perl x hi at x line 5.
Re: Signals and die message
by Scarborough (Hermit) on Feb 24, 2005 at 16:45 UTC
    If you realy wont to replace die you may like to try something simple like this.
    open IN, "nofile1" or &kill("message"); sub kill{ print "HANDLE the message"; #And if you need to exit from here }

    Or is this missing something?