I'm using Data::Dumper
to print a perl hash with configuration, which will be evaluated by another script. The problem is that it always prints $VAR =
at the start of output. I tried settings the Varname
parameter to empty string, but then i get $1
instead of $VAR
. How do I prevent printing the variable name using Dumper
?
my $params = {-PARAMS => 0} #data
local $Data::Dumper::Purity = 1;
local $Data::Dumper::Varname = "";
print Dumper($params) ;
Prints:
$1 = {
'-UPDATE' => 0,
}
I want to have:
{
'-UPDATE' => 0,
}
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.
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.
The Perl print hash can used $ symbol for a single hash key and its value. The Perl print hash can use the % symbol for multiple hash keys and their values.
Simply set $Data::Dumper::Terse = 1;
and it should work:
$ perl -MData::Dumper -wle '$Data::Dumper::Terse = 1; print Dumper {-PARAMS => 1}'
{
'-PARAMS' => 1
}
Or use the OO syntax:
print Data::Dumper->new([ {-PARAMS => 1 } ])->Terse(1)->Dump;
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