Hello victorz22,

I tried using eval...

Actually, this is a job for the do EXPR syntax (see do). With data file dir/hash_file.txt under the script’s directory and its contents corrected as per kevbot’s answer, the following script:

use strict; use warnings; use Data::Dumper; our %hash; cycleUsesForLibs('./dir', 'Scope', 'hash_file.txt', 'model'); sub cycleUsesForLibs { my ($fileDirectory, $scopeName, $fileName, $modelName) = @_; chdir $fileDirectory; do $fileName; my @hashData = $hash{$scopeName}{$modelName}; my @dereferencedData; push @dereferencedData, @$_ for @hashData; print "Data: \n", Dumper(@dereferencedData); }

gives this output:

14:35 >perl 1777_SoPW.pl Data: $VAR1 = 'data I need'; $VAR2 = 'another row of needed data'; $VAR3 = 'more data I need'; 14:36 >

Note:

  1. With use strict in place (as it always should be!), it’s necessary to declare our %hash before referencing it in $hash{$scopeName}{$modelName}.
  2. You want @dereferencedData to contain all the lines of data, not just the last one, so you need to push these onto the array. The code you show just overwrites the array on each iteration of the foreach loop.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Accessing variables in an external hash without eval by Athanasius
in thread 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.