Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent printing the variable name using `Data::Dumper`

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,
}
like image 563
Mariusz Jamro Avatar asked Mar 27 '12 08:03

Mariusz Jamro


People also ask

What is Data:: Dumper in Perl?

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.

How do I print a variable in Perl?

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.

How do I print an entire hash in Perl?

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.


2 Answers

Simply set $Data::Dumper::Terse = 1; and it should work:

$ perl -MData::Dumper -wle '$Data::Dumper::Terse = 1; print Dumper {-PARAMS => 1}'
{
  '-PARAMS' => 1
}
like image 109
Sebastian Stumpf Avatar answered Oct 22 '22 17:10

Sebastian Stumpf


Or use the OO syntax:

print Data::Dumper->new([ {-PARAMS => 1 } ])->Terse(1)->Dump;
like image 6
ysth Avatar answered Oct 22 '22 18:10

ysth