Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Reading a hash structure stored in a file

by haukex (Archbishop)
on Jan 15, 2019 at 21:19 UTC ( [id://1228621]=note: print w/replies, xml ) Need Help??


in reply to Reading a hash structure stored in a file

The main problem is that my declares a lexical variable, meaning it is restricted to its lexical scope. evaled code has its own lexical scope, so the surrounding code can't see the %hash1 declared inside the evaled code. One possible way to fix this issue would be to append some code to the string that you are evaling that returns the values, so for example something like my $hash1ref = eval($code."; return \\%hash1;");

However, eval will execute arbitrary code, and if you use it for untrusted code, it might allow an attacker to inject any code into your system, which is of course a bad thing. One possible way around this is with my module Config::Perl, which will parse data structures like the one you showed. For example:

use warnings; use strict; use Config::Perl; use Data::Dumper; my $parser = Config::Perl->new; my $data = $parser->parse_or_die('file.pl'); my %hash1 = %{ $data->{'%hash1'} }; print Dumper(\%hash1); __END__ $VAR1 = { 'hi' => 2, 'hello' => 1 };

Another issue with your original code is that you need to give eval a string, not a filehandle. A simple way to slurp a file into memory is my $code = do { local $/; <$fh> };. Also, if you really want to use eval, you should check it for errors, in the above example by checking if $hash1ref is defined or not. Update: Also, instead of evaling a file, there's do. But as I said, I wouldn't recommend these methods.

Replies are listed 'Best First'.
Re^2: Reading a hash structure stored in a file
by Anonymous Monk on Jan 22, 2019 at 00:34 UTC
    On top of all of those problems, the string eval will run at runtime, which is far too late for the strict 'vars' check to find out that %hash1 has been declared.
      the string eval will run at runtime, which is far too late for the strict 'vars' check to find out that %hash1 has been declared

      Did you mean "hasn't been declared"? If yes, then note that the strict check still kicks in, although as you said, at runtime. If you meant it as written, then this doesn't make sense to me... use strict 'vars'; has nothing to do with variables being declared twice (that's just a warning, in the shadow category, and it doesn't happen if the variables are in different scopes).

      $ perl -wMstrict -le 'eval "%hash1=(); 1" or warn "<<$@>>"' <<Global symbol "%hash1" requires explicit package name (did you forge +t to declare "my %hash1"?) at (eval 1) line 1. >> at -e line 1. $ perl -wMstrict -le 'my %hash1; my %hash1; print "Foo";' "my" variable %hash1 masks earlier declaration in same scope at -e lin +e 1. Foo $ perl -wMstrict -le 'my %hash1; { my %hash1; } print "Foo";' Foo $ perl -wMstrict -le 'my %hash1; eval "my %hash1;"; print "Foo";' Foo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-23 22:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found