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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|
|