in reply to Re^3: is there a way to ensure some code is the last thing that is run? (updated garbage collection)
in thread is there a way to ensure some code is the last thing that is run?

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

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

Replies are listed 'Best First'.
Re^5: is there a way to ensure some code is the last thing that is run?
by LanX (Saint) on Feb 03, 2017 at 10:00 UTC
    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!