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

I was wondering why it is that you don't seem to be able to use die in a DESTROY sub.

It's probably my fault, I've got into the habit of using die whenever I want to report an error and exit. In this case of course, my program was already in the process of exiting which is why the DESTROY got called, but I was still surprised to find that when in the event of an error happening and I used die inside the DESTROY, my error message did not print. Easily fixed by printing to STDERR instead, but I was wondering if others were aware of this behaviour and if someone had as explanation as to why it happens (or doesn't in this case!).

Cheers!

  • Comment on die & DESTROY - but not at the same time?

Replies are listed 'Best First'.
Re: die & DESTROY - but not at the same time?
by sauoq (Abbot) on Aug 14, 2003 at 01:19 UTC

    Actually, it is dying. It's just doing it within an eval. Check $@ to find the message you died with. Here's an example:

    $ perl -le 'sub DESTROY { die "dead" }; { bless \my $o } print $@;' (in cleanup) dead at -e line 1.

    Edit: Minimalized the example.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Thanks, that makes sense. So where is the eval? Is that deep inside the mysterious workings of Perl OO land?

        Uh... well, it's a special block... so it's... uh... handled "specially". <insert lots of handwaving>

        Frankly, I'd be more than happy to listen to a real explanation from someone who knows. :-)

        By the way, as of 5.6, dies within a DESTROY are visible if you run with warnings enabled. Here's the relevant passage from perldoc perl56delta:

        Failures in DESTROY() When code in a destructor threw an exception, it went unnoticed + in ear- lier versions of Perl, unless someone happened to be looking in + $@ just after the point the destructor happened to run. Such failures +are now visible as warnings when warnings are enabled.
        And another example:
        $ perl -wle 'sub DESTROY { die "dead" }; { bless \my $o }' (in cleanup) dead at -e line 1.

        Update: Also worth noting, the entry from perldoc perldiag:

        (in cleanup) %s (W misc) This prefix usually indicates that a DESTROY() met +hod raised the indicated exception. Since destructors are usua +lly called by the system at arbitrary points during execution, +and often a vast number of times, the warning is issued only on +ce for any number of failures that would otherwise result in the s +ame message being repeated.
        That implies that if DESTROY prints the same thing each time, the warning will only be printed once. If the warning is different each time, however, it should be printed each time. More examples to substantiate that:
        $ perl -wle 'sub DESTROY { die "dead" }; { bless \my $o; bless \my $p +}' (in cleanup) dead at -e line 1. $ perl -wle 'sub DESTROY { die "dead: $_[0]"}; { bless \my $o; bless \ +my $p }' (in cleanup) dead: main=SCALAR(0x805f0d4) at -e line 1. (in cleanup) dead: main=SCALAR(0x805f164) at -e line 1.

        Edit: Replaced

        $ perl -wle 'my $c=0; sub DESTROY { die "dead: ",$c++; }; { bless \my +$o; bless \my $p }' (in cleanup) dead: 0 at -e line 1. (in cleanup) dead: 1 at -e line 1.
        with a better example.

        -sauoq
        "My two cents aren't worth a dime.";