No, the syntax you quote does not create a copy.
This expression:
%{$hash_ref}
exactly equivalent to:
%$hash_ref
and assuming that the $hash_ref scalar variable $hash_ref indeed contain a hash reference, adding % at the front is just “dereferencing” the link — that is, it resolves the value representing the main hash (the thing $hash_ref ).
If you look at the documentation for the each function, you will see that it expects a hash as an argument. Putting % in the foreground is how you provide the hash when you have hashref.
If you wrote your own routine and passed the hash to it like this:
my_sub(%$hash_ref);
then at some level you could say that the hash was "copied", since inside the subprogram, the special @_ array will contain a list of all key / value pairs from the hash. However, even in this case, the @_ elements are actually aliases for keys and values. You would really get a copy if you did something like: my @args = @_ .
The Perl builtin each function is declared with the prototype '+', which efficiently uses the hash (or array) argument in reference to the underlying data structure.
As an aside, starting with version 5.14, each function can also refer to a hash. Therefore, instead of:
($key, $value) = each(%{$hash_ref})
You can simply say:
($key, $value) = each($hash_ref)
source share