in reply to Initializing an anonymous hash

I don't know how you are getting your data from the file read, but you can avoid the eval by massaging your input string creatively, then just add them to the anonymous hash.
#!/usr/bin/perl -w use strict; my $hash = {'MONTH' => 'January'}; print $hash->{MONTH} . "\n"; my $string = q('MONTH' => 'January'); #from file # I split your original string, but there probably is a cleaner # way from your file read my @split = split / => /, $string; s/'//g for @split; print "@split\n"; my $hash1->{ $split[0] } = $split[1]; print $hash1->{MONTH} . "\n";
Output:
$ ./962633.pl January MONTH January January

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Initializing an anonymous hash
by Anonymous Monk on Mar 31, 2012 at 07:45 UTC
    I was being lazy. The data file at the moment is quite safe. But that can change. I guess I could write small function to parse the string and create the hash and return it.