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!