5.6.3. Discussion
The methods differ in the degree that their output is customizable
(in order and formatting) and in their efficiency.
The first method, iterating over the hash, is flexible and
space-efficient. You can format the output as you like it, and it
requires only two scalar variables: the current key and value. You
can print the hash in key order (at the cost of building a list of
sorted keys) if you use a foreach loop:
foreach $k (sort keys %hash) {
print "$k => $hash{$k}\n";
}
The map function is just as flexible. You can
still process the list in any order by sorting the keys. You can
customize the output to your heart's content. But it builds up a list
of strings like "KEY
=>VALUE\n" to pass to print.
The last two methods are interpolation tricks. By treating the hash
as a list, you can't predict or control the output order of key-value
pairs. Furthermore, the output will consist of a list of keys and
values, each separated by whatever string that $"
happens to hold. You can't put newlines between pairs or
"=>" within them, as we could with the other
methods.
Another solution is to print the hash in a list context after
temporarily localizing the $, variable to a space.
{
local $, = " ";
print %hash;
}
This is like the solution of copying to an array and then doing
double-quote interpolation on that array, except it doesn't duplicate
the contents of the hash twice more than you need (i.e., once for the
array, then again for the string).