pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to do some persistence, and Data::Dumper seems like a popular tool, so that's what I'm trying to use.

I'm using LWP::Simple to grab web pages, and then I create a hash with a few tidbits about the web page and a copy of the text from the web page. I'd like to stash that hash away somewhere. I've tried playing around with Data::Dumper, but it's giving me a headache.

$hash_ref->{name} = 'a page'; $hash_ref->{time_to_download} = '3.23453'; $hash_ref->{content} = $content; # from LWP::Simple get my $dumped = Dumper($hash_ref); print $a_file $dumped;

I've tried setting Data::Dumper::Pure and Data::Dumper::Terse.

Then there's the problem of eval'ing this thing later. I don't want to eval the entire text of a web page. Maybe the text is 'system("rm -r $HOME")', or however you'd actually format that.

Thanks... gotta run.. another $%^#$^ fire alarm
-Pileofrogs

Update: Thanks! I'll go check out those other modules!

Another Update: YAML looks perfect! I especially like the 'taint safe' aspect.

Final Update: YAML solved my problem. I also used MIME::base64 to encode the content from LWP::Simple before YAML'ing it. That was a big performance boost.

Replies are listed 'Best First'.
Re: Data::Dumper for a Dummy
by jeffa (Bishop) on Dec 08, 2005 at 20:25 UTC

    I'd stay away from using Data::Dumper as a persitence tool, even though i recently posted some code that did just that. As was pointed out in that thread, how about using Storable or YAML instead?

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: Data::Dumper for a Dummy
by GrandFather (Saint) on Dec 08, 2005 at 20:32 UTC

    As you have a hash ref XML::Simple may be a good option.


    DWIM is Perl's answer to Gödel
Re: Data::Dumper for a Dummy
by gam3 (Curate) on Dec 08, 2005 at 20:54 UTC
    You are evaling the hash, not the content.
    use Data::Dumper; my $content = q[<head><body>'body'</body>]; $hash{'name'} = 'a page'; $hash{'time_to_download'} = '3.23453'; $hash{'content'} = $content; my $bob = Dumper \%hash; # $bob = $VAR1 = { name => 'a page', # time_to_download => 3.23453', # content => '<head><body>\'body\'</body>'; # }; eval $bob; print Dumper $VAR1;
    After you eval $bob you will find that $VAR1 holds the original hash. As long as the hash does not refer to itself you don't need to use should not need to set any flags.

    I would agree that Data::Dumper is not the best tool for this, but it does work easily with simple hashes, and can be made to work with quite complex hashes.

    -- gam3
    A picture is worth a thousand words, but takes 200K.