in reply to Re^4: 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?

Yes I just updated my post.

here the demo:

package Blubb; + sub new { my $this = bless {}; # $this->{circular} = $this; } sub DESTROY { warn "DESTROY\n"; } package MAIN; #{ my $b = Blubb->new; #} END { warn "END\n"; }

DESTROY END

What's surprising me is that it already works without curlies. I expected the variable to be accessible in END, like in BEGIN.


The only chance I see is to have a second dummy variable which is guaranteed to be destroyed later and to put the code into it's destructor.

Hauke, I'm sure you want to try this out! ;-)

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

Replies are listed 'Best First'.
Re^6: is there a way to ensure some code is the last thing that is run?
by ikegami (Patriarch) on Feb 03, 2017 at 18:25 UTC
    Remember, there's a lexical scope around the file, so
    package Blubb; ... package MAIN; my $b = Blubb->new; END { warn "END\n"; }
    is the same as
    { package Blubb; ... package MAIN; my $b = Blubb->new; END { warn "END\n"; } }

    Since nothing captures $b, it ceases to exists as soon as the end of the lexical scope (the end of the file) is reached. If a sub were to capture $b, it would keep the variable alive until the sub was freed. This is usually during global destruction.

Re^6: is there a way to ensure some code is the last thing that is run?
by haukex (Archbishop) on Feb 03, 2017 at 10:53 UTC

    Hi Rolf,

    The only chance I see is to have a second dummy variable which is guaranteed to be destroyed later and to put the code into it's destructor. Hauke, I'm sure you want to try this out! ;-)

    Actually, I was thinking in a different direction: eliminating the circular references in the first place :-) See my node here.

    Regards,
    -- Hauke D

        Hi ikegami,

        Ah, you are correct, Thank you.

        Regards,
        -- Hauke D

Re^6: is there a way to ensure some code is the last thing that is run?
by LanX (Saint) on Feb 03, 2017 at 10:29 UTC
    > What's surprising me is that it already works without curlies. I expected the variable to be accessible in END, like in BEGIN.

    Interesting Perl get's it right, when needed.

    package Blubb; sub new { my $this = bless {a=>42}; # $this->{circular} = $this; } sub DESTROY { warn "DESTROY\n"; } package MAIN; my $b = Blubb->new; END { warn $b->{a}; warn "END\n"; }

    42 at c:/tmp/pm/destruct_end.pl line 20. END DESTROY

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