Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Re: Module to read a dumped file

by juo (Curate)
on Feb 11, 2003 at 15:02 UTC ( [id://234410]=note: print w/replies, xml ) Need Help??


in reply to Re: Module to read a dumped file
in thread Module to read a dumped file

I have checked out the Data::Dumper and indeed it allows to dump a hash table and it is slightly in a different structure then the dumpvalue does but I could not find how to read back a dumped file. Like dumpvalue it only goes in one direction.

use Data::Dumper; open (STDOUT, ">whatever.dat"); print Dumper(%flexlm); close STDOUT;
$VAR1 = 'dataParams'; $VAR2 = { 'license_feature' => { 'val' => 'geditor' }, 'total_licenses' => { 'val' => '15' }, 'machine_names' => { 'parmOptions' => [ 'solw0328', 'solw0082', 'vasw1006', 'lav000021453a', 'tczw5216', 'CRKW388', 'vasw1005' ] }, 'user_names' => { 'parmOptions' => [ 'solwebef', 'solwebef', 'vaskhall', 'lvlvalor', 'tczrale', 'crkjcree', 'vasplund' ] }, 'licenses_used' => { 'val' => 7 }, 'license_server' => { 'val' => '10.210.134.126' } };

Replies are listed 'Best First'.
Re: Re: Re: Module to read a dumped file
by broquaint (Abbot) on Feb 11, 2003 at 15:28 UTC
    I could not find how to read back a dumped file
    Like so
    use Data::Dumper; my %hash = qw( foo one bar two baz three ); open DATA, ">whatever.dat"; print DATA Dumper(\%hash); close DATA; my %hash2 = %{ do "whatever.dat" }; print Dumper(\%hash2); __output__ $VAR1 = { 'foo' => 'one', 'baz' => 'three', 'bar' => 'two' };
    For more info see the ever-magical do() function.
    HTH

    _________
    broquaint

      That it was that simple after all. This is doing the job except for one thing. If your hashtable contains a non-referenced top level it will be removed during the input using do. See sample below

      Original hash table

      $VAR1 = 'dataParams'; $VAR2 = { 'license_feature' => { 'val' => 'geditor' }, };

      New hash table

      $VAR1 = 'license_feature'; $VAR2 = { 'val' => 'geditor' };

      So it is not exactly the same but it already helps a lot.

        Try using the Dump function of Data::Dumper like this:
        use Data::Dumper; my %hash2 = ( some => "really", complicated => ["hash", ["structure"]] ); print Data::Dumper->Dump([\%hash2], ['*hash2']);
        this prints out:
        %hash2 = ( 'some' => 'really', 'complicated' => [ 'hash', [ 'structure' ] ] );
        writing to a file and do()ing it is left as an exercise

        --

        flounder

        If you're going to be using a simple do() then you need to pass a reference to Dumper() as passing a bare hash results in a list of values being dumped which is not what you want e.g
        use Data::Dumper; my %hash = ( 'dataParams', { 'license_feature' => { 'val' => 'geditor' } } ); open DATA, ">whatever.dat"; print DATA Dumper(%hash); close DATA; print "bare hash dump\n", Dumper(do "whatever.dat"); open DATA, ">whatever.dat"; print DATA Dumper(\%hash); close DATA; print "reference dump\n", Dumper(do "whatever.dat"); __output__ bare hash dump $VAR1 = { 'license_feature' => { 'val' => 'geditor' } }; reference dump $VAR1 = { 'dataParams' => { 'license_feature' => { 'val' => 'geditor' } } };
        So as you can see when you dump by reference you get back the complete data structure.
        HTH

        _________
        broquaint

        You are saying listify the hash first ala
        print Dumper(%hash);
        When you should be saying dump the hash as a reference
        print Dumper(\%hash);
        But as someone said, if you want to dump several top level vars and get them back in a list form
        $dump= Data::Dumper->Dump([[\%hash,\@array,$scalar]],[qw(*vars)]); my @vars=eval $dump;
        theres a few other tricks as well. You can parse out the var names from dumper then wrap them all in a do and return them as a list, yada yada.

        A last point. You may want to dump in Pruity=1 mode.

        $Data::Dumper::Purity=1; print Data::Dumper->new([\%foo],['*foo'])->Purity(1)->Dump;
        Be aware that seriously sick and twisted data structures dont dump properly with Data::Dumper. For this reason unless hand modification is needed of the dumped data you should use Storable instead. Also, evaling these structres from a file is a serious security risk (hostile code can be embedded in the file and will execute with your permissions when you eval it). Another good reason to use Storable (which has an easy to use reader too.)

        Anyway,

        --- demerphq
        my friends call me, usually because I'm late....

Re: Re: Re: Module to read a dumped file
by ropey (Hermit) on Feb 11, 2003 at 15:18 UTC
    Once you have read it in from the file and stored in a scalar, just do
    my $ref = eval $data;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://234410]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 17:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found