Hello Monks, I am trying to load in separate files and access variables inside a hash in those files. The function I am making needs to take in the file name, file location, and two keys of that hash as parameters. The files will change so I am trying to code this without 'require'ing them in the beginning of the program. I tried using eval but I get an error where when I try and store the hash data in an array, the data ends up being from the wrong file.
#The external hash looks like this %hash = ( #scope changes Scope => { model => [ data I need another row of needed data more data I need ] } IrreleventScope =>{ #don't need this stuff } )
#My function looks like this sub cycleUsesForLibs{ my ($fileDirectory, $scopeName, $fileName, $modelName) = @_; chdir($fileDirectory); open FILE_HANDLE, "$fileName" or die "couldn't open file '$fileNam +e'. \n"; my $loadedFile = join "", <FILE_HANDLE>; close FILE_HANDLE; eval $loadedFile; my @hashData = $hash{$scopeName}{$modelName}; my @dereferencedData; foreach my $line (@hashData){ @dereferencedData = @$line; } print "Data: \n"; print Dumper @dereferencedData; print "\n"; #die "Couldn't interpret the file ($loadedFile) that was given.\nE +rror details follow: $@\n" if $@; }
In reply to Accessing variables in an external hash without eval by victorz22
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |