Perl Data Dumper Specifiers Explanation

355 Views Asked by At

Observing the output of Data::Dumper, the specifiers ($VAR1, "", ;) are not explained in the CPAN documentation.

  1. What is the purpose for the $VAR1?
  2. What is the purpose for the semicolon?
  3. What is the purpose for the quotations?

Here is my output:

$VAR1 = "Snow";
$VAR1 = "Rain";
$VAR1 = "Sunny";
$VAR1 = "";
2

There are 2 best solutions below

1
On BEST ANSWER

Looks like you have an array:

my @arr = ('Snow','Rain','Sunny');
print Dumper(@arr);

When you pass the array, Dumper thinks you passed 3 separate variables. That is why you get:

$VAR1 = 'Snow';
$VAR2 = 'Rain';
$VAR3 = 'Sunny';

In order to see the array as data structure, you need to pass the reference to the array:

print Dumper(\@arr);

This will produce:

$VAR1 = [
          'Snow',
          'Rain',
          'Sunny'
        ];

The output says that you passed the reference to array with 3 elements.

0
On

The specifiers are described in the second paragraph of the DESCRIPTION:

The return value can be "eval"ed to get back an identical copy of the original reference structure.

So, you can take the string returned by Dumper and run

my $x = eval $dumped_string;