|Syntactic Confectionery Delight|
Re: Pass by value acts like pass by referenceby gmpassos (Priest)
|on Jul 12, 2004 at 07:19 UTC||Need Help??|
The question is not your hash that is changed, actually your hash is still the same, a group of pairs of keys and references to arrays. The question is the references that are in the values of your hash.
When you write
you are creating an anonimous reference to an array, that is the same to write:
So, when you paste a hash as a argument to the sub, the hash is converted to an array, where we have a sequence of keys and values. The scalars that build this array (@_) will exists only inside the sub, but what you forgot is that the scalars are holding a reference to an array, and your main hash, outside the sub, is also holding this references to the same arrays. So, you are changing the same arrays and this is the meaning of references, this is why they exists.
The best way to understand is forgeting the hash and make a simple test with references:
So, if you paste the reference you change the main array, but if you work over $ref2 and redefine it (last line) this won't change $main_ref, that still hold the reference. Now just think that the values of your hash work exaclty as this 2 scalars.
Graciliano M. P.