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.