Ask a Question related to PERL Beginners, Design and Development.
-
Jan Eden #1
Data::Dumper trouble
Hi all,
I want to store some complex data. My first solution made use of Storable, which worked just fine. Now I learned that neither Storable nor MLDBM are available on the machine I want to use the program on (and they cannot be made available).
So I retreat to the core and use Data::Dumper to write a reference to my data to a file ("..." indicates an omitted hash of arrays of hashes):
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
my %monate = ...
open OUT, "> monate.dbm";
print OUT Dumper(\%monate);
close OUT;
Next I try to restore the data. My first attempt is this:
open IN, "monate.dbm";
my $temp = join '', <IN>;
my $reimport = eval $temp;
Unfortunately, $reimport has the same value as $temp:
$VAR1 = ...
Reading the Data::Dumper doc? Good idea. I found the $Data::Dumper::Terse variable, which made Data::Dumper leave out the variable name $VAR1.
Great, it works. But, from the perldoc
Hm. In my case, setting $Data::Dumper::Terse was absolutely necessary to make eval work the way I wanted it to. Is there another way to get the original data structure into a variable? In this context, I must admit that the different behaviour of eval's two forms (eval EXPR and eval BLOCK) is a bit mysterious for me.>$Data::Dumper::Terse or $OBJ->Terse([NEWVAL]) When set,
>Data::Dumper will emit single, non-self-referential values as
>atoms/terms rather than statements. This means that the $VARn names
>will be avoided where possible, but be advised that such output may
>not always be parseable by "eval".
I am grateful for hints.
Thanks,
Jan
--
A common mistake that people make when trying to design something completely foolproof is to underestimate the ingenuity of complete fools.
Jan Eden Guest
-
ANNOUNCE: Code::Dumper - a ::Dumper for code. Available on CPAN
NAME Code::Dumper - Debugging module to have your cake and eat it too SYNOPSIS use Code::Dumper; # DUMP ( print "just another perl hacker"; -
Data::Dumper && null values
This code: #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $buf = pack('C4',0,0,0,0); print Dumper($buf); -
Data::Dumper->dump()ing a hash?
When I use the following code to dump a hash: $entry{"genre"} = $genre; $entry{"artist"} = $artist; $entry{"album"} = $album; $entry{"disc"} =... -
Data::Dumper How to quote keys while dumping
I am trying to dump a hash using Data::Dumper. I need to quote the keys while dumping. My progarm looks like this use Data::Dumper;... -
Data::Dumper vs perl -d
>>>>> "Bill" == Bill Smith <wksmith@optonline.net> writes: Bill> What advantages of the module approach am I overlooking? Data::Dumper can be... -
Rob Dixon #2
Re: Data::Dumper trouble
Jan Eden wrote:
Hi Jan.>
> I want to store some complex data. My first solution made use of Storable, which
> worked just fine. Now I learned that neither Storable nor MLDBM are available on
> the machine I want to use the program on (and they cannot be made available).
>
> So I retreat to the core and use Data::Dumper to write a reference to my data to
> a file ("..." indicates an omitted hash of arrays of hashes):
>
> #!/usr/bin/perl -w
>
> use strict;
> use Data::Dumper;
>
> my %monate = ...
>
> open OUT, "> monate.dbm";
> print OUT Dumper(\%monate);
> close OUT;
>
> Next I try to restore the data. My first attempt is this:
>
> open IN, "monate.dbm";
> my $temp = join '', <IN>;
> my $reimport = eval $temp;
>
> Unfortunately, $reimport has the same value as $temp:
>
> $VAR1 = ...
>
> Reading the Data::Dumper doc? Good idea. I found the $Data::Dumper::Terse
> variable, which made Data::Dumper leave out the variable name $VAR1.
>
> Great, it works. But, from the perldoc
>>> >$Data::Dumper::Terse or $OBJ->Terse([NEWVAL]) When set,
> >Data::Dumper will emit single, non-self-referential values as
> >atoms/terms rather than statements. This means that the $VARn names
> >will be avoided where possible, but be advised that such output may
> >not always be parseable by "eval".
> Hm. In my case, setting $Data::Dumper::Terse was absolutely necessary to make
> eval work the way I wanted it to. Is there another way to get the original data
> structure into a variable? In this context, I must admit that the different
> behaviour of eval's two forms (eval EXPR and eval BLOCK) is a bit mysterious for
> me.
Just
my $reimport = do 'monate.dbm' or die $!;
will do what you want. It will work with or without 'Terse'.
HTH,
Rob
Rob Dixon Guest
-
Marcos Rebelo #3
RE: Data::Dumper trouble
Dont do;
my $temp = join '', <IN>;
do
my $temp = do {local $/; <IN>};
is faster.
For serialize, I use this code:
sub serializeSimple($)
{
my ($object) = @_;
my $Dumper = Data::Dumper->new([$object]);
$Dumper->Indent(0);
$Dumper->Purity(1);
$Dumper->Terse(1);
my $dump = $Dumper->Dump;
return $dump;
}
to unserialize
sub unserializeSimple($)
{
my ($text) = @_;
return eval($text =~ /^\{/ ? '+'.$text : $text);
}
to clone
sub clone($)
{
return unserializeSimple(serializeSimple($_[0]));
}
I did this code some time ago but it works for me.
Marcos
-----Original Message-----
From: Rob Dixon [mailto:rob@dixon.port995.com]
Sent: Friday, February 13, 2004 1:38 PM
To: [email]beginners@perl.org[/email]
Subject: Re: Data::Dumper trouble
Jan Eden wrote:which>
> I want to store some complex data. My first solution made use of Storable,available on> worked just fine. Now I learned that neither Storable nor MLDBM areavailable).> the machine I want to use the program on (and they cannot be madedata to>
> So I retreat to the core and use Data::Dumper to write a reference to mymake> a file ("..." indicates an omitted hash of arrays of hashes):
>
> #!/usr/bin/perl -w
>
> use strict;
> use Data::Dumper;
>
> my %monate = ...
>
> open OUT, "> monate.dbm";
> print OUT Dumper(\%monate);
> close OUT;
>
> Next I try to restore the data. My first attempt is this:
>
> open IN, "monate.dbm";
> my $temp = join '', <IN>;
> my $reimport = eval $temp;
>
> Unfortunately, $reimport has the same value as $temp:
>
> $VAR1 = ...
>
> Reading the Data::Dumper doc? Good idea. I found the $Data::Dumper::Terse
> variable, which made Data::Dumper leave out the variable name $VAR1.
>
> Great, it works. But, from the perldoc
>>> >$Data::Dumper::Terse or $OBJ->Terse([NEWVAL]) When set,
> >Data::Dumper will emit single, non-self-referential values as
> >atoms/terms rather than statements. This means that the $VARn names
> >will be avoided where possible, but be advised that such output may
> >not always be parseable by "eval".
> Hm. In my case, setting $Data::Dumper::Terse was absolutely necessary todata> eval work the way I wanted it to. Is there another way to get the originaldifferent> structure into a variable? In this context, I must admit that themysterious for> behaviour of eval's two forms (eval EXPR and eval BLOCK) is a bitHi Jan.> me.
Just
my $reimport = do 'monate.dbm' or die $!;
will do what you want. It will work with or without 'Terse'.
HTH,
Rob
--
To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
For additional commands, e-mail: [email]beginners-help@perl.org[/email]
<http://learn.perl.org/> <http://learn.perl.org/first-response>
Marcos Rebelo Guest



Reply With Quote

