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

Destruction happens at the end of scope not file.

Try to put curlies around the code excluding the END.

In other words make sure the variables are in another scope than the END block.

UPDATE

Tested, doesn't work with your code!

BUT the problem is your circular reference.

It means that a normal destruction can't happen because the ref-count keeps up.

We are talking here about the garbage collection at the very end of a Perl script..

UPDATE
(on exit Perl 5 does a mark and sweep to reclaim circular references.)

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

  • Comment on Re^3: is there a way to ensure some code is the last thing that is run? (updated garbage collection)

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

    Hi LanX,

    Try to put curlies around the code excluding the END.

    Unfortunately, that doesn't seem to work in this case. I assume that because of the circular reference, the object really is the very last thing to be destroyed, even after the END block.

    Regards,
    -- Hauke D

      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!

        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.

        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

        > 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!