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

Hi all.

I'm trying to read in a hash reference dumped to a file with Data::Dumper via an eval with no success. After much frustration I've discovered this works fine in the debugger, but not in the code. I was hoping someone could explain why.

I know there are better ways to do this (Storable, etc.) but I need a plain text version of the data and, more importantly, I'd like to know why this happens differently in the debugger.

#perl v5.10.1 my $testdata; open(DATA, "<filename") or die $!; { local $/; $DB::single=1; $testdata = eval <DATA>; } close DATA; #testdata will be undefined
However, when using the debugger:
DB<2> $testdata = eval <DATA>; DB<3> x $testdata #outputs the expected data
Thanks for your time.

Replies are listed 'Best First'.
Re: Reading in a dumped hash reference works in the perl debugger but not in the code.
by LanX (Saint) on Mar 15, 2012 at 16:44 UTC
    DATA is a predefined keyword, plz use something different. (but not END ;-)

    see Special Literals in perldata

    Cheers Rolf

      Thanks for the tip LanX, I'll keep that in mind. :)

      Using NOTDATA for the file handle yields the same results

Re: Reading in a dumped hash reference works in the perl debugger but not in the code.
by Ken Slater (Initiate) on Mar 15, 2012 at 17:06 UTC

    Not sure if I have enough info, but the following seems to work for me on Windows XP, ActiveState Perl 5.8.9.

    use strict; use warnings; use Data::Dumper; my $filename = 'hashref.txt'; my $hashRef = {a => '1', b => '2'}; open my $FH, '>', $filename or die "Unable to open $filename for output: $^E\n"; print $FH Dumper $hashRef; close $FH; my $testdata; # Since I'm using 'strict', must declare $VAR1 as it is used in # the dump file. my $VAR1; open $FH, '<', $filename or die $!; { local $/; # Not sure what this line is for # $DB::single=1; $testdata = eval <$FH>; } close $FH; while (my ($key,$value) = each(%$testdata)) { print "$key $value\n"; } # # This is what the file hashref.txt looks loke # __DATA__ $VAR1 = { 'a' => '1', 'b' => '2' };

    HTH, Ken

      Thanks Ken. Unfortunately $testdata is still undefined when I use your code to read in my file. What's strange is that the dumped file seems perfectly valid. If I take a copy of the dumped data from the file and paste it into the code and compare it against the hash generated before it's dumped it works just fine.