alakamale
alakamale

Reputation: 23

Data::Dumper Perl module

What does the following statement actually mean?

use Data::Dumper qw(Dumper);

Is Data::Dumper only used for printing contents of a hash?

Could you please explain in brief?

Upvotes: 1

Views: 9401

Answers (3)

serenesat
serenesat

Reputation: 4709

In simple words, Data::Dumper takes a Perl data structure and turns it into a string containing Perl code that, when eval()ed (that is, run as code), returns an equivalent data structure. This is useful for many reasons, but a very simple use is to print out a data-structure to see if it matches what you think it is.

Example:

use Data::Dumper qw(Dumper); # Import the Dumper() subroutine
my %hash = ( a => 1, b => 2, c => 3 );
print Dumper(\%hash);  # Note the \ backslash; Dumper() takes references as arguments

Output:

$VAR1 = {
          'c' => 3,
          'a' => 1,
          'b' => 2
        };

It is taken from Re: Data::Dumper in reference to Hashes.

Upvotes: 3

GreenAsJade
GreenAsJade

Reputation: 14685

The specific statement use Data::Dumper qw(Dumper) means "include the Dumper list of features from the Data::Dumper module in the namespace of this script."

The documentation for Data::Dumper says:

"Given a list of scalars or reference variables, writes out their contents in perl syntax."

This is useful, because then you can load them back into another script at another time and rely on Perl itself to parse the text correctly into the perl structures you had before.

Upvotes: 1

Xtof
Xtof

Reputation: 222

Dumper method takes a list of scalar or reference ... reference can be from different type hashref, arrayref

It is a nice way for debugging complex structure

SCALAR (not really useful indeed)

[~]=> perl -e 'use Data::Dumper; $a=1; print Dumper($a);'
$VAR1 = 1;

ARRAYREF

[~]=> perl -e 'use Data::Dumper; @a=(1,2,3,4); print Dumper(\@a);'
$VAR1 = [
          1,
          2,
          3,
          4
        ];

HASHREF

[~]=> perl -e 'use Data::Dumper; %a=(a=>1,b=>2,c=>3); print Dumper(\%a);'
$VAR1 = {
          'a' => 1,
          'c' => 3,
          'b' => 2
        };

COMPLEX HASHREF

[~]=> perl -e 'use Data::Dumper; %a=(a=>1,b=> ["apple","banana","pear"],c=> {z=>99,y=>98}); print Dumper(\%a);'
$VAR1 = {
          'c' => {
                   'z' => 99,
                   'y' => 98
                 },
          'b' => [
                   'apple',
                   'banana',
                   'pear'
                 ],
          'a' => 1
        };

ARRAYREF and HASHREF

[~]=> perl -e 'use Data::Dumper; @a=("zidane", "platini", "kopa"); %b=(a=>1,b=>2,c=>3); print Dumper(\@a, \%b);'
$VAR1 = [
          'zidane',
          'platini',
          'kopa'
        ];
$VAR2 = {
          'a' => 1,
          'c' => 3,
          'b' => 2
        };

ARRAYREF with cycle

[~]=> perl -e 'use Data::Dumper; %a=(x=>1, y=>\%a);  print Dumper(\%a);'
$VAR1 = {
          'x' => 1,
          'y' => $VAR1
        };

Upvotes: 0

Related Questions