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

Re^2: hash question

by duyet (Friar)
on May 27, 2016 at 05:55 UTC ( [id://1164261]=note: print w/replies, xml ) Need Help??


in reply to Re: hash question
in thread hash question

Thanks all for your prompt responses. I don't understand why the address has been changed between "Sa:" and "Sb:" by assigning another anonymous hash. I expected "Sb:" would be the same as "Sa:", since i was using the same var. I changed the code a bit:
sub do_something { my $h = shift; print '1. do_something(): ' . $h . "\n"; %{ $h } = ( a => 'alpha', b => 'beta' ); print '2. do_something(): ' . $h . "\n"; print Dumper( $h ); }
and it shows:
hash = HASH(0x490d30) 1. do_something(): HASH(0x490d30) 2. do_something(): HASH(0x490d30) { 'a' => 'alpha', 'b' => 'beta' } { 'a' => 'alpha', 'b' => 'beta' }
The address is not changed ... the behaviour is not consistent to me :)

Replies are listed 'Best First'.
Re^3: hash question
by andal (Hermit) on May 27, 2016 at 07:44 UTC

    You are missing one slight catch. When you do

    my $hash = shift;
    you get pointer to the object passed to the function. When you do
    $hash = {a => "alpha"};
    you create new object and store its pointer in the variable that previously contained pointer to another object. Finally, when you do
    %{ $hash } = ( a => 'alpha', b => 'beta' );
    then you take object pointed by the variable and store in that object new set of keys.

    You have to distinguish "objects" and "pointers to objects", the latter are called "references" in perl, then things may become more consistent for you.

Re^3: hash question
by soonix (Canon) on May 27, 2016 at 07:00 UTC
    You are printing the content of the variable. It's two different variables. If you print the variables directly instead of via Dumper,
    my $hash = {}; do_something( $hash ); print Dumper( $hash ); sub do_something { my $hash = shift; $hash = { a => 'alpha', b => 'beta', }; print Dumper( $hash ); }
    you'll get two different addresses:
    HASH(0x3c4a4)HASH(0x3c39c)
    Update: actually, this is also only the contents. Best answer so far to your OP is this by AnomalousMonk
Re^3: hash question
by AnomalousMonk (Archbishop) on May 27, 2016 at 16:12 UTC
    I don't understand why the address has been changed between "Sa:" and "Sb:" by assigning another anonymous hash. I expected "Sb:" would be the same as "Sa:", since i was using the same var. [Emphasis added]

    That's like saying

    In the code
        $x = 4;
        $x = 7;
    I don't understand why the value of  $x is 7 after the second assignment to $x: I'm using the same variable.

    As others have noted, it's vital to distinguish between a variable, the content of the variable, and, if the content of the variable is a reference, the referent of the reference. References are tricky.


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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (1)
As of 2024-04-25 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found