Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-20 01:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found