Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: hash question

by AnomalousMonk (Archbishop)
on May 27, 2016 at 18:41 UTC ( [id://1164352]=note: print w/replies, xml ) Need Help??


in reply to hash question

Somewhat belatedly, it occurs to me to wonder if you have some confusion between the notions of reference and alias. Of course, an alias is just another name for a single thing. Neo is the alias in the Matrix of Thomas Anderson in the "real" world — whatever that is! Whatever happens to Neo in the Matrix happens to Anderson in the real world. *

Using aliasing, I can come up with a code example that behaves rather as you seem to expect the OPed code to behave. (Note that in Perl, the function argument list is always a list of aliases.)

c:\@Work\Perl>perl -wMstrict -MData::Dumper -le "my $hash = {}; print 'Ma: ', $hash; ;; do_something($hash); ;; print 'Mb: ', $hash; print Dumper($hash); ;; sub do_something { print 'Sa: ', $_[0]; ;; $_[0] = { a => 'alpha', b => 'beta', }; print 'Sb: ', $_[0]; print Dumper( $_[0] ); } " Ma: HASH(0x92d06c) Sa: HASH(0x92d06c) Sb: HASH(0x92d16c) $VAR1 = { 'a' => 'alpha', 'b' => 'beta' }; Mb: HASH(0x92d16c) $VAR1 = { 'a' => 'alpha', 'b' => 'beta' };
In this example, the value of the (aliased) reference changes as one would expect between points Sa and Sb in the subroutine, and also changes in exactly the same way between points Ma and Mb in the "real" world (as does the referent).

Update: * I think I got that wrong. Thomas Anderson is the alias in the Matrix for Neo, the real person extracted from his pod by Morpheus. Remember that Morpheus says to Neo "welcome to the desert of the real." (Jeeze, I gotta get a life...)


Give a man a fish:  <%-{-{-{-<

Replies are listed 'Best First'.
Re^2: hash question
by duyet (Friar) on Jun 03, 2016 at 12:57 UTC
    Thanks! I guess i was confused between reference and alias.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2024-04-18 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found