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

thank you all for your help-- Broquaint -- I am using the 'do' command as you suggested, but when I try to retrieve a value, it comes out empty. I think the problems is that is doesn't recongnise the %dict hash. I tried using %VAR and it didn't work either.

Any suggestions?

## testing to see if the 'do' command worked: my $dict = do "a.pl" or die "could not load for reading: $!, $@"; my $test = $VAR{anode}{def}; print "test: $test\n";

--------------------------------------

I am without my perl books (I'm not in the US right now) and I can't seem to find any good example of creating a hash, 'dumping' it to a file and then reading the hash in a completely separate program and accessing the values of the original hash.

I've found tons of examples of creating hashes and dumping them (and i was successfully able to do it) -- but I'm having so much trouble using it in a different program. What's the point of dumping all this data to a file if I can't use it quickly and efficiently in a different program?(!)

In my first program I created a hash and wrote it to a file (see below) -- it successfully writes the hash to a file. Can someone help me 'load' this data into a completely different program?

Also, I am creating one file for each letter of the alphabet, but when I load the data in my new program I would like the entire alphabet to be loaded as one hash. Is this possible?:

########################### use Data::Dumper; $Data::Dumper::Purity = 1; my @alph = ('a'..'z'); my $hw; my $def; my $pos; for ($a=0; $a < 1; $a++) ## only doing one letter ## until we get dumper ## to work properly #for ($a=0; $a < @alph; $a++) { $cidefile = "cide.".$alph[$a]; ## the file we're reading from $newfilename = $alph[$a] . ".pl"; open(C,$cidefile) || die "\ncould not open $cidefile for reading\n"; open (N,">$newfilename") || die "could not open $newfilename\n"; @file = <C>; close(C); for ($i=0; $i < @file; $i++) { if ($file[$i] =~ /<hw>(.+?)<\/hw>/) { $hw = lc($1); $hw =~ s/\*|\"|\'|\`//gi; if ($file[$i] =~ /<def>(.+?)<\/def>/) { $def = $1; $def =~ s/<.*?>//g; } if ($file[$i] =~ /<pos>(.+?)<\/pos>/) { { $pos = $1; } ## save the part of speech and the definition $dict{$hw}{pos} = $pos; $dict{$hw}{def} = $def; } } print N Data::Dumper->Dump([%dict],['dict']); close(N); }

Replies are listed 'Best First'.
Re: Data Dumper Examples
by broquaint (Abbot) on Nov 03, 2003 at 10:13 UTC
    If you change the line where you dump the data structure to
    print N Dumper(\%dict);
    You can load the dumped hash with a simple do e.g
    my $dict = do "cide.$alpha[$a].pl" or die "Couldn't load dump: $!, $@";
    This will load the hashref that was dumped into $dict.
    HTH

    _________
    broquaint

      Broquaint -- I am using the 'do' command as you suggested, but when I try to retrieve a value, it comes out empty. I think the problems is that is doesn't recongnise the %dict hash. I tried using %VAR1 and it didn't work either. Any suggestions?
      ## testing to see if the 'do' command worked: my $dict = do "a.pl" or die "could not load for reading: $!, $@"; my $test = $dict{'anode'}{def}; print "test: $test\n";
        Make certain you're dumping a single value. This is because do is evaluating the file and returning the last thing evaluated, which in the case of a single dumped hashref should be the hashref itself e.g
        shell> perl -MData::Dumper -e 'my %h = qw/foo bar baz quux/; print Dumper \%h' > h.dump shell> cat h.dump $VAR1 = { 'foo' => 'bar', 'baz' => 'quux' }; shell> perl -MData::Dumper -e 'my $h = do "h.dump"; print Dumper $h' $VAR1 = { 'foo' => 'bar', 'baz' => 'quux' };
        Note that I'm using a hash ref there, not a hash. You'll also want to turn on $Data::Dumper::Terse if you're running with strict 'vars' as it'll choke on $VAR1.
        HTH

        _________
        broquaint

Re: Data Dumper Examples
by DrHyde (Prior) on Nov 03, 2003 at 10:09 UTC
    To read a file you've written using Data::Dumper, slurp it into a scalar (see perldoc perlvar and search for $/), then eval that scalar.

    #include std_warning_about_security_and_eval

    I, however, prefer to use Storable for such things.

      Or you could do it a better way...

      --
      3dan

Re: Data Dumper Examples
by Abigail-II (Bishop) on Nov 03, 2003 at 10:50 UTC
    If you want to use a hash in one program, have its data be stored on disk so that you can use the data in another program as well, you might be much better off with one of the DB* files instead of a serializer like Data::Dumper (which I seldomly use anyway. If I want debugging output, I vastly prefer YAML, whose results are much more readable and compact than Data::Dumpers).

    Abigail