http://www.perlmonks.org?node_id=981196


in reply to Trying to access hash from external perl file

Well, there are a couple methods, but none is particularly clean from my perspective. The right™ way to do this is to edit File_a.pl to be a module (perlmod), and interact with it programmatically. However, given your spec, I can see two ways to proceed.

  1. If the assignment to %test_hash occurs in the main package and the hash is not lexical (declared with my, see Private Variables via my()), you can use the do mechanism to literally execute the file. Obviously, this will also invoke all the many side effects of the script, which may be undesirable.
  2. You could open the file, strip out the relevant declaration, perhaps using a regex, and then feed it into an eval.

Both of these feel fragile to me, but they will do the job.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.