Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Recreating hash from Data::Dumper text

by theguvnor (Chaplain)
on Apr 14, 2004 at 21:38 UTC ( #345212=perlquestion: print w/replies, xml ) Need Help??

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

I have been trying to eval a text file that I previously dumped to file with Data::Dumper. I had no luck until I lifted stricture with a no strict.

Could someone explain this to a very tired monk? I'm sure there is solid logical reason for it, but the diagnostics message wasn't making particularly much sense to me after a full day in front of the screen.

Update: never mind, I see now. Because use strict; doesn't like the undeclared $VAR1 from the file, the assignment fails. I didn't see this earlier but I see this now by adding a print $@ if $@; right after the eval(). Like I said, I'm tired ;-) Thanks to the swift replies that confirmed this (silly) oversight on my part.

Here's (the original) reduced problemset:

use strict; use diagnostics; my $h; # hashref { #no strict; # no output until un-commented!! local $/; my $t=<DATA>; $h=eval $t; } foreach my $item ( keys %{$h} ) { print "$item\n"; } exit; __DATA__ $VAR1 = { 'ITEM1' => { 'A' => { 'count' => 4, 'percentage' => '1', 'qty' => 4 } }, 'ITEM2' => { 'A' => { 'count' => 15, 'percentage' => '1', 'qty' => 193 } } };


Replies are listed 'Best First'.
Re: Recreating hash from Data::Dumper text
by kvale (Monsignor) on Apr 14, 2004 at 21:50 UTC
    When you eval the Dumper data, $VAR1 barfs under use strict. Try
    $h=eval 'my '. $t;
    This trick only works for a single $VAR1. You will want no strict for more general cases.


Re: Recreating hash from Data::Dumper text
by Stevie-O (Friar) on Apr 14, 2004 at 22:02 UTC
    Incidentally, if you're merely trying to save a structure to a disk file, you should take a look at Storable; it's more space-efficient and doesn't need to be fed to 'eval' (always a risky proposition) for deserialization. Plus, I think it's core (though I'm uncertain when it became so).
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
      E:\>corelist Storable -a
      Storable  was first released with perl 5.007003
        5.007003   1.015
        5.008      2.04
        5.008001   2.08
        5.008002   2.08
        5.009      2.08

      MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
      I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
      ** The third rule of perl club is a statement of fact: pod is sexy.

      Thanks for the tip; I looked briefly into Storable and it appears it does use eval internally. From the documentation:

      Since Storable version 2.05, CODE references may be serialized with the help of B::Deparse. To enable this feature, set $Storable::Deparse to a true value. To enable deserializazion, $Storable::Eval should be set to a true value. Be aware that deserialization is done through eval, which is dangerous if the Storable file contains malicious data. You can set $Storable::Eval to a subroutine reference which would be used instead of eval. See below for an example using a Safe compartment for deserialization of CODE references.

      Still useful, but subject to the caveats with any use of eval.


Re: Recreating hash from Data::Dumper text
by tilly (Archbishop) on Apr 15, 2004 at 02:34 UTC
    If you've dumped the data to a file, you can just do the file and avoid a lot of code and hassle.

    If efficiency matters, use Storable as suggested by others.

Re: Recreating hash from Data::Dumper text
by Stevie-O (Friar) on Apr 14, 2004 at 21:46 UTC
    My guess would be the $VAR1= conflicting with 'strict vars'...
    $"=$,,$_=q>|\p4<6 8p<M/_|<('=> .q>.<4-KI<l|2$<6%s!<qn#F<>;$, .=pack'N*',"@{[unpack'C*',$_] }"for split/</;$_=$,,y[A-Z a-z] {}cd;print lc
Re: Recreating hash from Data::Dumper text
by Plankton (Vicar) on Apr 14, 2004 at 22:24 UTC
    I had a similar problem. Take a look at this node and Coruscate reply to it. Hopefull you will find it helpful.

    Plankton: 1% Evil, 99% Hot Gas.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://345212]
Approved by kvale
Front-paged by broquaint
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2020-04-05 00:55 GMT
Find Nodes?
    Voting Booth?
    The most amusing oxymoron is:

    Results (33 votes). Check out past polls.