Neat puzzle!

  • $one is created and destroyed as we leave its scope, outputting one

  • $two is created but kept alive outside its block by the reference within closure

  • OnDestroy->say("four") is created and passed into closure

    • It gets assigned to $four

    • $three gets created

    • As we pass out of this scope, $four stays alive because of the reference in the newly created anonymous sub. There are no more references left to $three though, so it gets destroyed, outputting three

  • Now things get confusing ;) The anonymous sub executes, and the value of $two is replaced. Its old value gets destroyed, outputting two .. Its new value (same as the value in $four) stays around for the same reason $two is staying around in the first place.

  • The program prints five

  • The program ends, and the value in $two is finally garbage-collected. Due to the previous execution of that anonymous closure, the value of $two is the object which prints four!

Update: On my Perl 5.8.4 i386-linux-thread-multi, this outputs

one three five four two
which is different than PodMaster's output (probably due to undefined order of garbage collection at the end of the program), and certainly different from my guess. There's something fishy going on with the $two variable, I bet! Hmm.. Is the GC being lazy and deferring some destructions?

Update 2: dws tells me my guess is what he expected to happen, and what happens for him in 5.8.0. Bizarro!

Looks like perltoot says it all!

Perl's notion of the right time to call a destructor is not well-defined currently, which is why your destructors should not rely on when they are called.
Therefore I declare any permutation of one through five to be technically correct. I love happy endings! ;)

blokhead


In reply to Re: Know Your Garbage -- A Quiz by blokhead
in thread Know Your Garbage -- A Quiz by dws

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.