How can I print the contents of a hash in Perl?

Kys picture Kys · Jul 22, 2009 · Viewed 334k times · Source

I keep printing my hash as # of buckets / # allocated. How do I print the contents of my hash?

Without using a while loop would be most preferable (for example, a one-liner would be best).

Answer

tetromino picture tetromino · Jul 22, 2009

Data::Dumper is your friend.

use Data::Dumper;
my %hash = ('abc' => 123, 'def' => [4,5,6]);
print Dumper(\%hash);

will output

$VAR1 = {
          'def' => [
                     4,
                     5,
                     6
                   ],
          'abc' => 123
        };