Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I control the variable names in Perl's Data::Dumper?

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
       );
like image 348
raldi Avatar asked May 26 '09 02:05

raldi


People also ask

What is Data :: Dumper in Perl?

Data::Dumper. Converts Perl data structures into strings that can be printed or used with eval to reconstruct the original structures. Takes a list of scalars or reference variables and writes out their contents in Perl syntax.

What does $_ mean in Perl?

There is a strange scalar variable called $_ in Perl, which is the default variable, or in other words the topic. In Perl, several functions and operators use this variable as a default, in case no parameter is explicitly used.

How do I print a variable in Perl?

In any real-world Perl script you'll need to print the value of your Perl variables. To print a variable as part of a a string, just use the Perl printing syntax as shown in this example: $name = 'Alvin'; print "Hello, world, from $name.


2 Answers

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.

like image 139
ysth Avatar answered Sep 19 '22 22:09

ysth


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

like image 37
Chas. Owens Avatar answered Sep 18 '22 22:09

Chas. Owens