Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: passing a hash ref in a sub

by davido (Cardinal)
on Oct 31, 2018 at 15:13 UTC ( #1224997=note: print w/replies, xml ) Need Help??

in reply to passing a hash ref in a sub

When you call unfold_hash($data, $result_hr), the special variable @_ contains two elements, each that are aliases to the things you passed in. The first element is an alias to the variable that holds a reference to a datastructure. The second element is an alias to a variable that is not yet defined.

The first thing you do is then assign @_ to two variables: $raw_hr and $res_hr. At this point you have moved away from the aliasing. $raw_hr still contains a reference to the datastructure that was referred to by $data, but $res_hr just contains undef. You can think of it like this: Each variable is assigned a value. The value assigned to the first variable is a reference. The value assigned to the second variable is undef. The reference assigned to $raw_hr is the same reference that $data holds, so both of those variables independently refer to the same datastructure..

Aliasing no longer exists in these new variables. Yet you can modify the datastructure referred to by the reference that $raw_hr, and that change propagates outward, because they refer to the same data structure. On the other hand, if you assign a new reference to $raw_hr, it would not have anything to do with the $result_hr variable because no aliasing is in effect, and $result_hr does not know of this reference.

When you autovivify a hashref and assign it into $raw_hr there is no mechanism by which that hashref would propagate outward; there is no aliasing in effect here, and the reference was unknown outside of the subroutine. In the case where you set $result_hr = {}, you create a reference to an empty data structure, and the subroutine obtains a copy of that reference (not of the structure), which it can then manipulate such that changes to the structure propagate outward. ...this is high risk behavior, however, as it changes the data structure... anyone referring to it will have the same change, because it is the same structure.


Replies are listed 'Best First'.
Re^2: passing a hash ref in a sub
by jimpudar (Pilgrim) on Nov 01, 2018 at 17:13 UTC

    Fantastic explanation! The best documentation I have been able to find about this is perlsub, but I would love to see any more details if you have them.

    Here is the OP's code working as he expected:

    perl -e ' > use strict; > use warnings; > use Data::Dumper; > > my $data = {status=>"ok","message-type"=>"member","message-version"= +>"1.0.0"}; > my $result_hr; > > unfold_hash($data, $result_hr); > print Dumper $result_hr; > > sub unfold_hash { > for my $k ( keys %{ $_[0] } ) { > $_[1]{$k} = $_[0]{$k}; > } > print "unfold_hash ", $_[1] ? scalar %{ $_[1] } : 0, "\n"; > } > ' unfold_hash 3 $VAR1 = { 'message-version' => '1.0.0', 'message-type' => 'member', 'status' => 'ok' };

    πάντων χρημάτων μέτρον έστιν άνθρωπος.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2020-10-28 18:04 GMT
Find Nodes?
    Voting Booth?
    My favourite web site is:

    Results (263 votes). Check out past polls.