Data::Dumper trouble

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default 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
    >$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.

    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

  2. Similar Questions and Discussions

    1. 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";
    2. 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);
    3. 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"} =...
    4. 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;...
    5. 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...
  3. #2

    Default 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


    Rob Dixon Guest

  4. #3

    Default 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:
    >
    > 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]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

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139