Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: my and scope

by McA (Priest)
on Mar 28, 2013 at 21:05 UTC ( #1026040=note: print w/replies, xml ) Need Help??

in reply to my, scope, and references


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:

char* myfunc() { char* stringpointer; stringpointer = (char*) malloc(5000); if(stringpointer == NULL) { exit(1); } strcpy(stringpointer, "Hallo"); return(stringpointer); }
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.


Replies are listed 'Best First'.
Re^2: my and scope
by chayashida (Acolyte) on Mar 28, 2013 at 21:25 UTC

    Hi McA,

    I think you explained it perfectly. The last sentence is exactly what was confusing me, and the C example helped immensely.

    Thank you.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026040]
[davido]: wow, he's back again
[prathap keerthipati]: what are the possible cases for getting 500 cannot connection issue

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2017-03-23 05:50 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (282 votes). Check out past polls.