Dumper

G

George Mpouras

# this works



use Data::Dumper; $Data::Dumper::purity=1;
my $dmp;
my %hash1 = ('k1'=>'v1','k2'=>'v2');
$dmp = Data::Dumper::Dumper( \%hash1 );;
my $hash2 = eval $dmp; print Data::Dumper::Dumper($hash2);


# this is not

use strict; use warnings;
use Data::Dumper; $Data::Dumper::purity=1;
my $dmp;
my %hash1 = ('k1'=>'v1','k2'=>'v2');
$dmp = Data::Dumper::Dumper( \%hash1 );;
my $hash2 = eval $dmp; print Data::Dumper::Dumper($hash2);


what I am doing wrong
 
G

George Mpouras

# this works



use Data::Dumper; $Data::Dumper::purity=1;
my $dmp;
my %hash1 = ('k1'=>'v1','k2'=>'v2');
$dmp = Data::Dumper::Dumper( \%hash1 );;
my $hash2 = eval $dmp; print Data::Dumper::Dumper($hash2);


# this is not

use strict; use warnings;
use Data::Dumper; $Data::Dumper::purity=1;
my $dmp;
my %hash1 = ('k1'=>'v1','k2'=>'v2');
$dmp = Data::Dumper::Dumper( \%hash1 );;
my $hash2 = eval $dmp; print Data::Dumper::Dumper($hash2);


what I am doing wrong


after scratching my head I conclude to this

my $hash2 = eval "our $dmp \$VAR1";
print Data::Dumper::Dumper($hash2);

do you think its ok;
 
R

Rainer Weikusat

George Mpouras <[email protected]>
writes:

[...]
# this is not

use strict; use warnings;
use Data::Dumper; $Data::Dumper::purity=1;
my $dmp;
my %hash1 = ('k1'=>'v1','k2'=>'v2');
$dmp = Data::Dumper::Dumper( \%hash1 );;
my $hash2 = eval $dmp; print Data::Dumper::Dumper($hash2);


what I am doing wrong

One could argue that the code should check whether the eval succeeded,

use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::purity=1;
my $dmp;
my %hash1 = ('k1'=>'v1','k2'=>'v2');
$dmp = Data::Dumper::Dumper( \%hash1 );;
my $hash2 = eval $dmp;
die($@) if $@;
print Data::Dumper::Dumper($hash2);

[NB: Don't put more than one statement on a line. That's going to make
debugging rather painful in exchange for nothing tangible]

But very likely, you should rather be using

use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::purity=1;
$Data::Dumper::Terse=1;
my $dmp;
my %hash1 = ('k1'=>'v1','k2'=>'v2');
$dmp = Data::Dumper::Dumper( \%hash1 );;
print STDERR ($dmp);
my $hash2 = eval $dmp;
die($@) if $@;
print Data::Dumper::Dumper($hash2);

so that $dmp contains the Perl code necessary to create an anonymous
hash with the intended content instead of the $VAR1 = { ... } strict
disallows.
 
J

jl_post

# this works







use Data::Dumper; $Data::Dumper::purity=1;

my $dmp;

my %hash1 = ('k1'=>'v1','k2'=>'v2');

$dmp = Data::Dumper::Dumper( \%hash1 );;

my $hash2 = eval $dmp; print Data::Dumper::Dumper($hash2);





# this is not



use strict; use warnings;

use Data::Dumper; $Data::Dumper::purity=1;

my $dmp;

my %hash1 = ('k1'=>'v1','k2'=>'v2');

$dmp = Data::Dumper::Dumper( \%hash1 );;

my $hash2 = eval $dmp; print Data::Dumper::Dumper($hash2);





what I am doing wrong


Dear George,

By now you've figured out that the $VAR1 variable in the Data::Dumper::Dumper() output is conflicting with "use strict;", and that setting $Data::Dumper::Terse to 1 removes that conflict.

However, I question why you're dumping the output to Dumper into a string and then eval()ing it to assign to another hash. Is it because you want a convenient way to create a deep copy of a hash/array/object?

If so, I recommend using the Storable module instead. It's a standard module in Perl these days. It avoids the use of having to use eval(), and it's probably considerably quicker than doing the same thing with Data::Dumper.

Here I'm giving you three different ways to use the Storable module, oneof which should nicely match what you're trying to do:


{
# Making a copy from a temporary string (using freeze() and thaw()):
use strict;
use warnings;
my %hash1 = ('k1' => 'v1', 'k2' => 'v2');
use Storable;
my $tempString = Storable::freeze( \%hash1 );
my %hash2 = %{ Storable::thaw($tempString) };
use Data::Dumper; print Data::Dumper::Dumper( \%hash2 );
}


{
# Making a copy from a file (using nstore() and retrieve()):
use strict;
use warnings;
my %hash1 = ('k1' => 'v1', 'k2' => 'v2');
my $fileName = "file.tmp";
use Storable;
Storable::nstore( \%hash1, $fileName );
my %hash2 = %{ Storable::retrieve($fileName) };
use Data::Dumper; print Data::Dumper::Dumper( \%hash2 );
}


{
# Making a deep copy directly (using dclone()):
use strict;
use warnings;
my %hash1 = ('k1' => 'v1', 'k2' => 'v2');
use Storable;
my %hash2 = %{ Storable::dclone( \%hash1 ) };
use Data::Dumper; print Data::Dumper::Dumper( \%hash2 );
}


If all you want to do is to perform a deep copy on a hash/array/object, I advise using the third approach I listed (the one that uses Storable::dclone()). But if you want the data to a file to retrieve later, I recommend the approach that uses nstore() and retrieve().

Using eval() to copy data is not always a great idea. For one thing, ifyou the data that you eval() comes from a file, someone could have easily tampered with the contents of the file, placing malicious Perl code in the file that ends up getting executed when you eval() it.

So if all you want is to store data to a file (for later retrieval), useStorable::nstore() (and retrieve it later with Storable::retrieve()) instead of Data::Dumper. Or if all you want is to create a deep copy/clone of an object, use Storable::dclone().

The main reason you'd want to use Data::Dumper is to visually inspect the data you're working with, which is something you can't do easily with theStorable module (which is why I'm using Data::Dumper in all three of my examples above). (But if you only really needed to visually inspect the data, there'd be no reason to eval() it later.)

So my advice is to use the Storable module instead of Data::Dumper, unless you need to manually inspect the contents of a scalar/array/hash/object.

I hope this is helpful, George.

-- Jean-Luc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top