Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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.


In reply to Re: passing a hash ref in a sub by davido
in thread passing a hash ref in a sub by frazap

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (8)
    As of 2019-12-09 09:41 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found