in reply to What's the difference between a hash ?

The first way is straightforward, and probably desirable under many circumstances. The second way is kind of inefficient. In the second method, you're creating an anonymous hash, referred to by a temporary hashref. That hashref is then dereferenced as a hash, and assigned to %h2.

The second way takes an extra step. In the narrow context you've displayed it's completely unneeded. It's less memory and computationally efficient, and arguably less clear to the reader.

However, that's not to say there arent times when an anonymous hashref is useful. Consider the following:

my $href = { FOO => 1, BAR => 2 }; my_sub( $href ); sub my_sub { my $href = shift; # do something with the hash ref... }

In other words, references and anonymous hashes are, theselves, useful. But the way you used them in your second example isn't.

Relevant documentation: perlref, perlreftut, and perldsc.


Dave