Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^3: Pass array, then clear

by pryrt (Abbot)
on Jan 17, 2018 at 15:06 UTC ( [id://1207416]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Pass array, then clear
in thread Pass array, then clear

I did not suspect that the subroutine made a copy of the memory address.

ikegami++ just responded excellently, but I liked the earlier house key analogy, so want to expand on that.

$outside_data = ['a','b']; clear_it($outside_data); sub clear_it {
Grab a key unlocking a house containing two rooms; one room contains 'a', the other contains 'b'. Put that key in pocket#0, with pockets#1..n all empty
my($data)=@_;Grab the contents of pocket#0 (the key), create a duplicate, and put that duplicate in my pants pocket that I've named $data; ignore the contents of all other coat pockets (for now).
$data = [];Create a new key to an empty house. I want to put the new key into my pants pocket named $data, but that pants pocket was already full. Thus, empty the pocket and lose track of the duplicate key that I used to have in there (but I've still got the original key in my coat pocket#0). Now that my $data pocket is empty, it has room to hold the new key, so I put that key in.
}At this point, I actually empty the pants pockets and throw away the key to the new house. At some point, the perl garbage collectors will see a house with no keys, and demolish it, freeing up the property for future use. We don't really notice, because the garbage collectors work at night when no one is watching, and we're all waiting with baited breath to look inside a house.
print "size = ", scalar(@$outside_data)Take the key out of my coat pocket#0, unlock the house, and look at how many rooms are in the house it unlocks. We see 2 rooms, because it's still the original key, and we've never done anything to the inside of the original house.

alternately:

$outside_data = ['a','b']; clear_it($outside_data); sub clear_it {
(same as above)
my($data)=@_;(same as above)
@$data = ...Use the duplicate key that's in my $data pocket to unlock the door to the house.
... = ();Once inside the house, demolish everything inside that house and throw out all the rubble. It's the same house, it's just now empty inside. Any key that fits the house will still unlock the house.
}As before, I still throw away the $data key; it was a duplicate, and I still have the key in my coat pocket#0.
print "size = ", scalar(@$outside_data)Take the key out of my coat pocket#0, unlock the house, and look inside. We see the now-empty house, so it will say 0, because it's still the original house, which we "renovated".

I hope this helps paint a mental picture of what's going on.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 05:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found