in reply to Re: Data Dumper Examples
in thread Data Dumper Examples

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";

Replies are listed 'Best First'.
Re: Re: Re: Data Dumper Examples
by broquaint (Abbot) on Nov 03, 2003 at 11:45 UTC
    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