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:[EMAIL PROTECTED]
Sent: Friday, February 13, 2004 1:38 PM
To: [EMAIL PROTECTED]
Subject: Re: Data::Dumper trouble


Jan Eden wrote:
>
> 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.

Hi Jan.

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 PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to