Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
Do you know where your variables are?
 
PerlMonks  

a general question on references...

by derek3000 (Beadle)
on Jul 18, 2001 at 19:35 UTC ( [id://97851]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

derek3000 has asked for the wisdom of the Perl Monks concerning the following question:

Fellow monks,
I was just wondering what was going on behind the scenes in a particular situation. This might be a silly question, but if I have:

use strict; my $file = 'aFile'; my $hash_ref = get_hash($file); #blah blah blah sub get_hash{ my $file = shift; my %hash; #do something to populate a hash return \%hash; }

if %hash is scoped in that block, doesn't it die after it leaves the block? If that's true, wouldn't $hash_ref point to nothing? Just kind of curious.

Thanks,
Derek

Replies are listed 'Best First'.
Re: a general question on references...
by TheoPetersen (Priest) on Jul 18, 2001 at 19:41 UTC
    A lexical variable dies when its reference count goes to zero. Since you return a reference to %hash, it lives on :)
Re: a general question on references...
by lhoward (Vicar) on Jul 18, 2001 at 19:43 UTC
    Perl is smart enough to know that something is still referencing %hash and doesn't clean it up when leaving get_hash.
(Ovid - reference counts) Re: a general question on references...
by Ovid (Cardinal) on Jul 18, 2001 at 19:46 UTC

    Variables like %hash don't contain the data they represent. They point to it. So you are correct that %hash goes out of scope at the end of the sub, but the data that it points to is still there (in this example). Here's a simplified explanation as to why:

    Every time something references data contained in a variable, the reference count for the data is increased by one (it should be noted that the hash and the data that it contains maintain separate reference counts, so a hash could go out of scope but some of the data still remain). When the hash is created, it is assigned a reference count of one. When you return the reference to the hash and assign it to $hash_ref, its reference count increases by one, to two. When %hash goes out of scope, the reference count is decreased by one, to one. Since it still has a reference count of one, the data is still there.

    Only when $hash_ref goes out of scope (in the example above), will the reference count for the original hash drop to zero, leading Perl to remove all references to it (the data is not actually deleted, as this justs wastes time. It's merely inaccessible).

    Cheers,
    Ovid

    Vote for paco!

    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
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://97851]
Approved by root
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.