tags:

views:

115

answers:

3

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
       );
+14  A: 
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
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
Amazing what reading the documentation can do for you :)
ysth
+7  A: 

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

Chas. Owens
+1  A: 

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

AmbroseChapel
Beware the source filter. It bites.
Kent Fredric