in reply to Re: is there a way to ensure some code is the last thing that is run?
in thread is there a way to ensure some code is the last thing that is run?

Interesting stuff you bring up.

But just in order not to be misunderstood:

I provided the example just to illustrate how destructors can run after END-blocks.

In this particular case the problem is of couse the circular reference (that I put there simply to force this behaviour).

My question is not how to fix this particular case but if there was a way to ensure something is run at the very end, in particular when using some unknown code that you do not want to fiddle with.

  • Comment on Re^2: is there a way to ensure some code is the last thing that is run?

Replies are listed 'Best First'.
Re^3: is there a way to ensure some code is the last thing that is run? (updated)
by haukex (Archbishop) on Feb 03, 2017 at 17:03 UTC

    Hi morgon,

    My question is not how to fix this particular case but if there was a way to ensure something is run at the very end, in particular when using some unknown code that you do not want to fiddle with.

    Ah, I see - in that case it was still an interesting exercise. If you don't have circular references, then I believe END blocks are the answer, whereas if you do have circular references, that's what I might attack first. (Update: See Re^6: is there a way to ensure some code is the last thing that is run?)

    So, do I understand correctly you don't have a specific case where you're having problems, and this was a theoretical exercise?

    Regards,
    -- Hauke D

Re^3: is there a way to ensure some code is the last thing that is run?
by RonW (Parson) on Feb 06, 2017 at 20:03 UTC

    Morgon, interesting discussion you started.

    I think the best you can do is to focus on "seen first, run last" and "end of scope".

    Using those 2 points, I think the following is the closest to what you are asking for.

    #!perl # Make this END block the first thing compiled to insure it # will be the last END block to run. END { # any special clean up goes here warn 'END - Bypassing final global Perl clean up'; # $? is the value passed to exit() unless modified # in another END block. POSIX::_exit($?); # WARNING - Any DESTRUCTions that could not be # completed before _exit() is called will NOT # be completed } # Create an extra layer of scope { use strict; use warnings; use Some::Module; my $s = Some::Module->new(); } # Close extra scope to trigger most of the DESTRUCTion exit(0); # Here, the various END blocks will run in reverse order # of being compiled, concluding with the END block at the # top of this file.

    Disclaimer: Not tested. YMMV.