![]() |
|
Perl Monk, Perl Meditation | |
PerlMonks |
Re: my and scopeby McA (Priest) |
on Mar 28, 2013 at 21:05 UTC ( #1026040=note: print w/replies, xml ) | Need Help?? |
Hi, I try to explain in my own words: In the function you declare and instatiate an array @test_array. You now have one reference on a - lets call it - perl array object. Would you have done nothing with this array, than the last variable @test_array would have gone out of scope, the reference counter on that object would drop to zero and garbage collection would free the space needed for that array object. As soon as you return a reference to that object you increased the reference counter by one. The variable @test_array goes out of scope, but a single reference is still there, which points to an object that was created in the function. Here an example in C: You create space for an object with malloc and the variable stringpointer does have a reference on it. Then you return the address of stringpointer. The variable stringpointer itself will be removed from stack. But in the calling function you will have a reference on the object. To be precise: You have to differentiate between scope (what is seen where) and object instantiation and destruction. They can have the same boundaries but they need not to. McA
In Section
Seekers of Perl Wisdom
|
|