Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: hash question

by BrowserUk (Patriarch)
on May 26, 2016 at 11:00 UTC ( [id://1164185]=note: print w/replies, xml ) Need Help??


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?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

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

    No recent polls found