Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Dumper

Reply
Thread Tools

Dumper

 
 
George Mpouras
Guest
Posts: n/a
 
      10-15-2013
# this works



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


# this is not

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


what I am doing wrong
 
Reply With Quote
 
 
 
 
George Mpouras
Guest
Posts: n/a
 
      10-15-2013

> # this works
>
>
>
> use Data:umper; $Data:umper:urity=1;
> my $dmp;
> my %hash1 = ('k1'=>'v1','k2'=>'v2');
> $dmp = Data:umper:umper( \%hash1 );;
> my $hash2 = eval $dmp; print Data:umper:umper($hash2);
>
>
> # this is not
>
> use strict; use warnings;
> use Data:umper; $Data:umper:urity=1;
> my $dmp;
> my %hash1 = ('k1'=>'v1','k2'=>'v2');
> $dmp = Data:umper:umper( \%hash1 );;
> my $hash2 = eval $dmp; print Data:umper:umper($hash2);
>
>
> what I am doing wrong



after scratching my head I conclude to this

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

do you think its ok;


 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      10-15-2013
George Mpouras <(E-Mail Removed) m>
writes:

[...]

> # this is not
>
> use strict; use warnings;
> use Data:umper; $Data:umper:urity=1;
> my $dmp;
> my %hash1 = ('k1'=>'v1','k2'=>'v2');
> $dmp = Data:umper:umper( \%hash1 );;
> my $hash2 = eval $dmp; print Data:umper:umper($hash2);
>
>
> what I am doing wrong


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

use strict;
use warnings;
use Data:umper;
$Data:umper:urity=1;
my $dmp;
my %hash1 = ('k1'=>'v1','k2'=>'v2');
$dmp = Data:umper:umper( \%hash1 );;
my $hash2 = eval $dmp;
die($@) if $@;
print Data:umper:umper($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:umper;
$Data:umper:urity=1;
$Data:umper::Terse=1;
my $dmp;
my %hash1 = ('k1'=>'v1','k2'=>'v2');
$dmp = Data:umper:umper( \%hash1 );;
print STDERR ($dmp);
my $hash2 = eval $dmp;
die($@) if $@;
print Data:umper:umper($hash2);

so that $dmp contains the Perl code necessary to create an anonymous
hash with the intended content instead of the $VAR1 = { ... } strict
disallows.
 
Reply With Quote
 
George Mpouras
Guest
Posts: n/a
 
      10-15-2013
> $Data:umper::Terse=1;

perfect

 
Reply With Quote
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      11-06-2013
On Tuesday, October 15, 2013 6:36:07 AM UTC-6, George Mpouras wrote:
> # this works
>
>
>
>
>
>
>
> use Data:umper; $Data:umper:urity=1;
>
> my $dmp;
>
> my %hash1 = ('k1'=>'v1','k2'=>'v2');
>
> $dmp = Data:umper:umper( \%hash1 );;
>
> my $hash2 = eval $dmp; print Data:umper:umper($hash2);
>
>
>
>
>
> # this is not
>
>
>
> use strict; use warnings;
>
> use Data:umper; $Data:umper:urity=1;
>
> my $dmp;
>
> my %hash1 = ('k1'=>'v1','k2'=>'v2');
>
> $dmp = Data:umper:umper( \%hash1 );;
>
> my $hash2 = eval $dmp; print Data:umper:umper($hash2);
>
>
>
>
>
> what I am doing wrong



Dear George,

By now you've figured out that the $VAR1 variable in the Data:umper:umper() output is conflicting with "use strict;", and that setting $Data:umper::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:umper.

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:umper; print Data:umper:umper( \%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:umper; print Data:umper:umper( \%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:umper; print Data:umper:umper( \%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:umper. 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:umper 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:umper 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:umper, unless you need to manually inspect the contents of a scalar/array/hash/object.

I hope this is helpful, George.

-- Jean-Luc
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Data::Dumper for Java? Sam Java 4 02-11-2011 06:59 PM
OT: Blue screen descrbed by dumper Thor MCSE 3 07-12-2006 02:35 PM
Installation of Data::Dumper Eric Perl 0 06-09-2005 02:25 PM
Data::Dumper and XML::Simple in Java rc Java 3 08-16-2004 09:51 AM
Data::Dumper How to quote keys while dumping kamal Perl 0 08-12-2003 05:23 AM



Advertisments