Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

No! Perl uses reference counting, so an element of storage is eligible for garbage collection only when its reference count falls to zero. When you create the %result variable with my, its reference count is one. When you create a reference to it and push that reference onto @array, the reference count of the memory holding the data is incremented to 2. Then, when the for loop is exited iterates, the lexical variable %hash does indeed go out of scope, so the reference count of the memory it is using is decremented back to 1. But it’s still greater than 0, so the data won’t be garbage collected.

This is one of the features of Perl that make it a joy to work with. As a general rule, Perl is designed to “do the right thing”, which in this case means keeping the data around as long as it’s needed. Reference counting ensures that the data is still valid and available when it is later accessed via the @array variable.

But — try it to see!

Hope that helps,

Update 1: Improved wording slightly.
Update 2: Added link.
Update 3: s/is exited/iterates/.

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^3: Accessing HASH pushed into @array ('strict refs' in use error) by Athanasius
in thread Accessing HASH pushed into @array ('strict refs' in use error) by sbrothy

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 admiring the Monastery: (6)
As of 2024-03-29 13:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found