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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.