12

I've got this simple Perl script:

#! /usr/bin/perl -w

use strict;
use Data::Dumper;

my %foo = ( 'abc' => 1 );

print Dumper(\%foo);

It outputs:

$VAR1 = {
          'abc' => 1
        };

How do I make it output this instead?

%foo = (
         'abc' => 1
       );
Mariusz Jamro
  • 30,615
  • 24
  • 120
  • 162
raldi
  • 21,344
  • 33
  • 76
  • 86

4 Answers4

24
print Data::Dumper->Dump( [ \%foo ], [ qw(*foo) ] );

The extended syntax takes two arrayrefs: one of scalars to dump, and one of names to use. If the name is prefixed by * and the corresponding scalar is an arrayref or hashref, an array or hash assignment is produced.

ysth
  • 96,171
  • 6
  • 121
  • 214
  • Ah, I had tried sticking everything I could think of (or copy-paste from the perldoc) in between the parentheses, but I hadn't realized that the problem was I was using Dumper() instead of Data::Dumper->Dump(). – raldi May 26 '09 at 03:17
9
use Data::Dumper;

$Data::Dumper::Terse = 1;

print '%foo = '.(Dumper \%foo);
serenesat
  • 4,611
  • 10
  • 37
  • 53
Michel
  • 109
  • 1
  • 1
8

In addition to ysth's answer, you can use Ovid's Data::Dumper::Names module.

Chas. Owens
  • 64,182
  • 22
  • 135
  • 226
2

Also, Data::Dumper::Simple does roughly that.

serenesat
  • 4,611
  • 10
  • 37
  • 53
AmbroseChapel
  • 11,957
  • 7
  • 46
  • 68