in reply to reduce reference count of hash

When you are considering curiosities like this, be very careful to properly consider what the “reference count” you are looking at belongs to. For instance, in your example there are three players in the game:

  1. The variable "$hash."
  2. The variable "$retval."
  3. A hash... being a value, floating in space.

To Perl's memory-manager, all three of these separate things are distinct. The lifetime of variables is, of course, determined by their scope, but “a variable” is distinct from “the value it ‘contains.’” To put it another way, a variable holds a reference to the thing that it contains, until it (the variable) goes out-of-scope.

If you set variable X to be “a reference to” another variable Y, then change the value of the referenced variable Y to some other value, you will observe that X continues to be a reference to the value that Y used to contain. That value continues to exist, because Y holds a reference to it.

References are a powerful part of Perl, but they can be confusing in-part because there is a certain amount of ambiguity:   the syntax implies that the reference is “to a variable,” when it's really a reference to a value. You don't usually think of values as being “things,” but Perl does.