Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Accessing variables in an external hash without eval

by Athanasius (Chancellor)
on May 17, 2017 at 04:48 UTC ( #1190423=note: print w/replies, xml ) Need Help??


in reply to Accessing variables in an external hash without eval

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,

Replies are listed 'Best First'.
Re^2: Accessing variables in an external hash without eval
by victorz22 (Sexton) on May 17, 2017 at 05:27 UTC

    Thank you however the code didn't work, i get an error saying i cant use an undefined value as an array reference on the line with the push statement. Also because my function will be called multiple times will the global %hash variable be overwritten? Im not sure how the global will work for multpile different hashes all using one global.

      i get an error saying i cant use an undefined value as an array reference on the line with the push statement.

      Then the hash wasn’t correctly read-in from the data file. Have you fixed the format of the data as kevbot showed? Without further details (see The SSCCE), it’s impossible to know where the problem lies.

      <Update>
      Add the error handling code shown at the end of the do documentation to determine whether the do statement (1) failed to read the file, (2) read the file but failed to parse (compile) it, or (3) successfully read and parsed the file. Then print out the contents of %hash to see exactly what data was read in.
      </Update>

      Also because my function will be called multiple times will the global %hash variable be overwritten?

      Yes, of course, each time the subroutine is called the do $fileName; statement will overwrite %hash with the data in the indicated file.

      Hope that helps,

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1190423]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2018-07-23 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (469 votes). Check out past polls.

    Notices?