Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Does Scope of Referenced Array Matter?

by Eliya (Vicar)
on Feb 19, 2012 at 22:34 UTC ( #954931=note: print w/ replies, xml ) Need Help??


in reply to Does Scope of Referenced Array Matter?

A reference to the array is stored in the global (package) variable $array_ref (which itself doesn't go out of scope).  This increments the reference count of @array, so the array doesn't "vaporize" even though it is scoped lexically to the routine.

You can use Devel::Peek to investigate the reference counts.  When you add

Dump $array_ref;

at the end of A(), and within B(), you'll see that the REFCNT is 2 within A(), but 1 within B()

# Dump from within A() ... SV = PVAV(0x138f058) at 0x13b4be0 REFCNT = 2 ... # Dump from within B() ... SV = PVAV(0x138f058) at 0x13b4be0 REFCNT = 1 ...

This is because when @array leaves its scope, the REFCNT is decremented.  If there was no reference kept to it, the REFCNT would go down to zero here, and the variable would be freed.  But due to the reference, this doesn't happen.


Comment on Re: Does Scope of Referenced Array Matter?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2014-10-01 22:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    What is your favourite meta-syntactic variable name?














    Results (40 votes), past polls