Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Data::Dumper and eval

by Anonymous Monk
on Nov 28, 2000 at 05:23 UTC ( #43585=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I have a script that uses Data::Dumper and prints the contents of a hash to a file:
print DATA Dumper( \%somehash );
When the script is rerun, it initializes that hash by reading the file:
open DATA, "<$datafile" or die "$!"; { local $/; %somehash = %{ eval <DATA> }; }
This works fine, but sometimes the datafile is empty. When that happens, I get the following message:
Can't use an undefined value as a HASH reference at somescript.pl line + 22, <DATA> chunk 1.
I tried to put the eval in a block to trap it:
%somehash = %{ eval { <DATA> } };
Unfortunately, I then get the following error message:
Ambiguous use of %{eval{...}} resolved to %eval{...} at somescript.pl +line 22. Global symbol "%eval" requires explicit package name at somescript.pl +line 22. syntax error at somescript.pl line 22, near "%{ eval { " syntax error at somescript.pl line 23, near "}" Execution of somescript.pl aborted due to compilation errors.
Anyone know how I can work around this? I thought about putting an empty {} in the file, but it would be nice to know what I'm doing wrong with the eval.

Thanks!

Comment on Data::Dumper and eval
Select or Download Code
Replies are listed 'Best First'.
Re: Data::Dumper and eval
by chromatic (Archbishop) on Nov 28, 2000 at 05:40 UTC
    If there's a possibility that your file is empty, you need an extra step:
    open DATA, "<$datafile" or die "$!"; my $dumped_hash; { local $/; $dumped_hash = <DATA>; } my %somehash; %somehash = %{ eval $dumped_hash } if $dumped_hash;
    That way, %somehash is empty and you'll avoid those errors if there's nothing in the file.

    Update: jcwren pointed out that I had an extra line that should have been edited out. I dare you to find it now.

Re: Data::Dumper and eval
by merlyn (Sage) on Nov 28, 2000 at 08:09 UTC
(tye)Re: Data::Dumper and eval
by tye (Cardinal) on Nov 28, 2000 at 05:56 UTC

    To add an eval to catch the error you are getting would look like:

    open DATA, "<$datafile" or die "$!"; { local $/; eval { %somehash = %{ eval <DATA> }; }; }
    but I'd do it differently. I'd check for the empty file separately so that a malformed file still generates an error that you can diagnose.

            - tye (but my friends call me "Tye")
Re (tilly) 1: Data::Dumper and eval
by tilly (Archbishop) on Nov 28, 2000 at 07:06 UTC
    I would do this rather differently:
    my $href = do $datafile; if (not $href) { if ($!) { die "Cannot eval $datafile: $!"; } else { $href = {}; } } my %hash = %$href;
Re: Data::Dumper and eval
by rpc (Monk) on Nov 28, 2000 at 05:31 UTC
    Caveat: I'm guessing you're not using strict? When testing some code like this, eval would die because $VAR1 was not declared.

    I would make your eval code a little more robust, to check for errors in the eval'd structure.

    use strict; use Data::Dumper; my %somehash; open DATA, "$ARGV[0]" or die "$!"; { local $/; my $code = <DATA>; my $VAR1 = eval $code; if($@) { die "eval: $@\n"; } %somehash = %$VAR1; } print Dumper(\%somehash);
    This is a nasty hack and not a good solution to persistance however ;)
Re: Data::Dumper and eval
by cwest (Friar) on Nov 28, 2000 at 05:28 UTC
    To solve your imidiate problem of Ambiguous use of ...
    %somehash = %{ +eval { <DATA> } };
    Enjoy!
    --
    Casey
       I am a superhero.
    

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2015-08-01 02:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls