The following will demonstrate an approach to define a &SUPEREND::DESTROY which is run after all other global DESTROY()s happend.

use strict; use warnings; package Blubb; sub new { my $this = bless {a=>42}; $this->{circular} = $this; } sub DESTROY { warn "DESTROY ". __PACKAGE__; } package SUPEREND; no warnings 'redefine'; my $orig_destroy= \&Blubb::DESTROY; my $persist = bless { count=>0}; *Blubb::DESTROY= sub { $orig_destroy->(@_); warn "Mini END"; $persist->{count}++; }; sub DESTROY { warn "DESTROY after $_[0]->{count} destroys ". __PACKAGE__; } package MAIN; my $b = Blubb->new; my $c = Blubb->new; my $d = Blubb->new;

DESTROY Blubb at c:/tmp/pm/destruct_end.pl line 12 during global destr +uction. Mini END at c:/tmp/pm/destruct_end.pl line 25 during global destructio +n. DESTROY Blubb at c:/tmp/pm/destruct_end.pl line 12 during global destr +uction. Mini END at c:/tmp/pm/destruct_end.pl line 25 during global destructio +n. DESTROY Blubb at c:/tmp/pm/destruct_end.pl line 12 during global destr +uction. Mini END at c:/tmp/pm/destruct_end.pl line 25 during global destructio +n. DESTROY after 3 destroys SUPEREND at c:/tmp/pm/destruct_end.pl line 33 + during global destruction.

Please note, that this can be extended to be a generic solution, because SUPEREND could parse all packages available and monkeypatch all DESTROYs there (if existent)

Since $persist is referenced it's DESTROY will be run last.

For syntactic sugar one could also make SUPEREND export a function SUPEREND(&) accepting a codeblock which is run by the DESTROY of $persist.

Cheers Rolf
(addicted to the Perl Programming Language and ☆☆☆☆ :)
Je suis Charlie!


In reply to Re^4: is there a way to ensure some code is the last thing that is run? (SUPEREND) by LanX
in thread is there a way to ensure some code is the last thing that is run? by morgon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.