Search code examples
perldata-dumper

Perl Data Dumper Specifiers Explanation


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 = "";

Solution

  • 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.