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

I Must have some crazy silly mistake in my code or my understanding. I don't quite get my expected output from the following very short script.

### die.pl #!/usr/bin/perl use strict; use warnings; $SIG{__DIE__} = sub { print "In die: '@_'"; }; print "Hello!\n"; die "Hi"; print "Goodbye!\n"; ### Result: [duelafn@teckla]$ ./die.pl Hello! In die: 'Hi at ./die.pl line 6. Hi at ./die.pl line 6. '[duelafn@teckla]$

Two things did not happen as expected. First, I expected execution to continue to print "Goodbye!\n";. Second, I don't get where the double message is coming from.

Any ideas or suggestions?

Update: Missed the inability to cheat death. Thanks! Any thoughts on the double message? - both messages appear between my single quotes!

Update 2: Nevermind, figured out the double message. Quotes are in the right place if I disable buffering $| = 1. Thanks all!

Good Day,
    Dean

Replies are listed 'Best First'.
Re: $SIG{__DIE__} not working as expected
by pc88mxer (Vicar) on Jun 09, 2008 at 00:29 UTC
    Perhaps this part of the perldoc -f die documentation explains what is going on:
        You can arrange for a callback to be run just before the "die"
        does its deed, by setting the $SIG{__DIE__} hook. ...
    
    You can change the message by die-ing again in your handler, but you cannot prevent death.

    If you need to continue, wrap your code in an eval block:

    eval { ... die "Hi!"; }; print "cheated death!\n";
      Or just override die:
      { local *CORE::GLOBAL::die = sub { print "Really die? "; if (($resp = <STDIN>) =~ /^y/i) { CORE::die @_; } else { print "narrowly averted: @_\n"; } }; # do the thing that dies }
Re: $SIG{__DIE__} not working as expected
by ferreira (Chaplain) on Jun 09, 2008 at 00:37 UTC
    Any ideas or suggestions?

    Read again the related documentation of die and $SIG{__DIE__}. In the last case, the documentation of $SGI{__DIE__} can be found at perlvar or if you have a brand new perldoc (like Pod-Perldoc-3.14_07), you can retrieve the docs that will help you with:

    $ perldoc -v '$SIG{expr}'

    The relevant piece is

    When a __DIE__ hook routine returns, the exception processing continues as it would have in the absence of the hook, unless the hook routine itself exits via a "goto", a loop exit, or a die().

    In other words, your current DIE handler does not stop the program from dying which explains the messages you are seeing.