Printing names with Perl's Data::Dumper output

By default the output of Perl's Data::Dumper module uses $VAR1, $VAR2 etc in place of the names of the variables passed in, which can make the output harder to read.

Calling its extended interface, passing an array of parameters to print, and an array of their names, gives nicer output. Take the following example:

use Data::Dumper;
my %data = (
          'foo' => {
                      'bar' => '1',
                      'badger' => 'mushroom'
                    }
        );

Calling Dumper(%foo) isn't very useful:

print Dumper(%foo)
# prints:
$VAR1 = 'foo';
$VAR2 = {
          'bar' => '1',
          'badger' => 'mushroom'
        };

Of course, to see the struture, we pass it in by reference instead:

print Dumper(\%data)
# prints:
$VAR1 = {
          'foo' => {
                     'bar' => '1',
                     'badger' => 'mushroom'
                   }
        };

... getting there, but the $VAR1 still isn't too helpful later on, so:

print Data::Dumper->Dump( [ \%data ], [ qw(*data ) ] );
# prints:
%data = (
          'foo' => {
                     'bar' => '1',
                     'badger' => 'mushroom'
                   }
        );

... that's better!

~~DISCUSSION~~

 
perl/datadumpernames.txt · Last modified: 2010/02/26 10:45 (external edit)
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki