Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Re: Re: Re: Module to read a dumped file

by juo (Curate)
on Feb 11, 2003 at 15:55 UTC ( #234429=note: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re^5: Module to read a dumped file
by flounder99 (Friar) on Feb 11, 2003 at 16:17 UTC
    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



Re: Re: Re: Re: Re: Module to read a dumped file
by broquaint (Abbot) on Feb 11, 2003 at 16:19 UTC
    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.


Re: Re: Re: Re: Re: Module to read a dumped file
by demerphq (Chancellor) on Feb 12, 2003 at 00:07 UTC
    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.)


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

      Yes indeed after looking at storable it looks as a much more suitable solution as the files only have to be used in Perl. However when looking at the function of storage it will store a Hash table into a file but it restores it into a scalar. I have been unable to get it back into a hash table.

      my %stages; $stages{job_choice}->{status}->{val}=""; $stages{job_choice}->{param_gui}->{val}=""; $stages{input}->{status}->{val}=""; use Storable; store \%stages, 'stages.test'; my $hashref = retrieve('stages.test'); # not good : result is a scalar my %hashref1 = %{do retrieve('stages.test')}; # This does the job but +it reports an error message.

      I have found the solution. They should actually add this to the storable documentation.

      my %hashref = %{retrieve('stages.test')};

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://234429]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-01-18 10:30 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (211 votes). Check out past polls.