in reply to hash question
In your first case, you initialise the scalar variable $hash with a reference to an empty anonymous hash: my $hash = {};
You then pass that reference into the sub: do_something( $hash );
Where is value (the reference) gets copied into another scalar my $hash = shift; (also called $hash, but a completely different piece of memory).
Then you assign another (completely different) reference to a different anonymous hash to that variable:
$hash = { a => 'alpha', b => 'beta', };
And then print the contents of that new hash; before returning to the main program and printing the contents of the first $hash, that hasn't changed since you initialised it.
In the second case in the sub, you assign (through the copy of the reference you passed in) to the original anonymous hash.
Thus, both print statements are printing the contents of the same anonymous hash, albeit that they are doing so through different copies of a reference to that hash.
Does that help?
|
|---|