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
);
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.
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.
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.
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.
In addition to ysth's answer, you can use Ovid's Data::Dumper::Names module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With