You may wish to access the hash as a whole, either to initialize it, or to copy it to another hash. Perl doesn't really have a literal representation for a hash, so instead it unwinds the hash as a list. Each pair of elements in the list (which should always have an even number of elements) defines a key and its corresponding value. This unwound representation can be assigned into another hash, which will then recreate the same hash. In other words:
@fred_list = %fred; # @fred_list gets ("aaa","bbb","234.5",456.7) %barney = @fred_list; # create %barney like %fred %barney = %fred; # a faster way to do the same %smooth = ("aaa","bbb","234.5",456.7); # create %smooth like %fred, from literal values
The order of the key-value pairs is arbitrary in this unwound representation and cannot be controlled. Even if you swap some of the values around and create the hash as a whole, the returned unwound list is still in whatever order Perl has created for efficient access to the individual elements. You should never rely on any particular ordering.
One quick use of this winding and unwinding is to copy a hash value to another hash variable:
%copy = %original; # copy from %original to %copy
And you can construct a hash with keys and values swapped using the
reverse operator, which works magically well here:
%backwards = reverse %normal;
Of course, if
%normal has two identical values, those will end up as only a single element in
%backwards, so this swap is best performed only on hashes with unique keys and values.