in reply to Initializing an anonymous hash
Unless the file is very locked down in terms of who is able to edit it, avoid using eval for this task like you'd avoid the plague.
An untrusted user could just put, say:
system("rm -fr $ENV{HOME}")... into the file, and when you eval the file, that line will be executed, deleting your home directory and all its contents.
The same incidentally goes for do and require. Unless you trust the file you're loading, don't do it.
Much better would be to store your hash in a file format specifically designed for storing that sort of data. Namely, JSON or YAML.
test.jsontest.pl:{ "MONTH": "January" }
use JSON qw(from_json); use File::Slurp qw(slurp); my $hash = from_json( ~~slurp('test.json') ); print $hash->{MONTH};
|
|---|