Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: (Ovid) Re: Creating a hash from two arrays

by wardk (Deacon)
on Mar 27, 2002 at 23:59 UTC ( #154849=note: print w/ replies, xml ) Need Help??


in reply to (Ovid) Re: Creating a hash from two arrays
in thread Creating a hash from two arrays

ovid,

One question...In the code you list, the hash is created via my in the sub, yet it's available outside the sub when the sub ends. I assume that it's not destroyed when the subroutine ends because it's still being referenced?

my first inclination would be to create the hash and pass a reference into the sub, assuming that would ensure it's availablity when the sub ends. (Aparently I am assuming wrong, and have learned a new thing! :)


Comment on Re: (Ovid) Re: Creating a hash from two arrays
(Ovid) Re(3): Creating a hash from two arrays
by Ovid (Cardinal) on Mar 28, 2002 at 00:58 UTC

    wardk asked:

    In the code you list, the hash is created via my in the sub, yet it's available outside the sub when the sub ends. I assume that it's not destroyed when the subroutine ends because it's still being referenced?

    Your assumption is correct. Variables are garbage collected when their reference count drops to zero. When the hash in the sub is declared with my, it has a reference count of one. When the reference to it is created, that reference has a reference count of one and the hash (now referred to as the "referent", or thing being referred to) has a reference of two. When the reference is returned, the original variable goes out of scope, setting the reference count down to one again.

    Note that there are two reference counts here. The $hash_ref variable has a reference count (at that point) of one and the referent (the hash we were pointing to) has a reference of one. Got that? It takes a while to get it all straight, but the process is fairly straightforward.

    wardk also wrote:

    my first inclination would be to create the hash and pass a reference into the sub, assuming that would ensure it's availablity when the sub ends.

    There are two difficulties with that approach. The first is that this is atypical behavior that tends to confuse newer programmers. For example, how many times have you seen someone ask what is wrong with this:

    $var = chomp $var;

    That usually sets $var to 1 (one) because chomp returns the number of characters removed. Most perl functions take an argument but don't change it.

    The other problem is code that quietly munges its arguments can cause all sorts of bugs for the unwary. For example, what if you passed in a hash that already had data and were expecting another one returned? That sort of stuff is very easy to misunderstand. Typical behavior for subroutines is to copy the arguments into new, non-aliased variables, do their stuff, and then return the results.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://154849]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2014-10-21 07:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (98 votes), past polls