Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Reading in a dumped hash reference works in the perl debugger but not in the code.

by Anonymous Monk
on Mar 15, 2012 at 15:48 UTC ( [id://959803]=perlquestion: print w/replies, xml ) Need Help??

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://959803]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2024-04-25 05:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found