Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

How to access a hash from another file

by vagabonding electron (Curate)
on Oct 14, 2011 at 11:44 UTC ( #931485=perlquestion: print w/replies, xml ) Need Help??

vagabonding electron has asked for the wisdom of the Perl Monks concerning the following question:

Dear All, while tinkering Re^3: How to declare variables per loop I faced a new problem.
The parameter hash which should be used further to generate a gnuplot command grows up to a huge thing since there are ca. 20 topics (as THIS, THAT etc.) with up to 12 indicators.
my %module = ( THIS => { name => q{This Great Evaluation}, indicator => { '01' => { ind => q{First}, legende => q{bottom}, yscala => q{[:100]}, }, '04' => { ind => q{Fourth}, legende => q{bottom}, yscala => q{[:100]}, }, '06' => { ind => q{Sixth}, legende => q{bottom}, yscala => q{[:100]}, }, '07' => { ind => q{Seventh}, legende => q{bottom}, yscala => q{[:100]}, }, '08' => { ind => q{Eighth}, legende => q{bottom}, yscala => q{[:100]}, }, '11' => { ind => q{Eleventh}, legende => q{bottom}, yscala => q{[:100]}, }, '14' => { ind => q{Fourteenth}, legende => q{top}, yscala => q{[0:]}, }, '15' => { ind => q{Fifteenth}, legende => q{top}, yscala => q{[0:]}, }, '16' => { ind => q{Sixteenth}, legende => q{top}, yscala => q{[0:]}, }, }, }, , );
I thought if it were possible to save this hash in a separate file and access it from the main file later.
In fact I tried to save the file as, say, and typed use lib (..Pfad..); and use; but I get an error.
Could you please give me an advice?
Many thanks.

Replies are listed 'Best First'.
Re: How to access a hash from another file
by jethro (Monsignor) on Oct 14, 2011 at 11:58 UTC

    As usual error messages tell you something. Read them instead of just acknowledging them. If you don't understand them directly, google them or find them in the documentation.

    In this case probably the error message told you that the evaluation of resulted in a false value, which should not be (although I don't have the slightest idea why this was built into the module system). Just add as a last statement anything that results to true, usually everyone just adds


    as the last line of a .pm file

      Sorry for incomplete message, jethro.
      In fact I did add the last statement 1; The error message says: Global symbol "%module" requires explicit package name at ... How should I name a hash from the module?
      As to:
      I don't have the slightest idea why this was built into the module system
      I simply cannot figure out how can I get it otherwise. One way is simply to let the hash in the main file, but as already said the hash grows huge and the main file will be confusing.
Re: How to access a hash from another file
by wfsp (Abbot) on Oct 14, 2011 at 12:40 UTC
    Use our instead of my as JavaFan suggests. You could also consider do
    my %hash = do q{path/};
    to read it into your script.
      Thank you very much wfsp, it works this way!
Re: How to access a hash from another file
by JavaFan (Canon) on Oct 14, 2011 at 12:19 UTC
    You create the hash as a lexical variable. Lexical variables are only accessable from within the smallest lexical scope they are created in. A file is a lexical scope. So, the answer is, you cannot.

    Having said that, you could create a pointer to the hash, and share the pointer - accessing the hash through the pointer. Or you replace my with our, and access the hash as you'd access any other package variable.

Re: How to access a hash from another file
by choroba (Cardinal) on Oct 14, 2011 at 12:46 UTC
    You may also use YAML or JSON to represent your hash in an external file.
Re: How to access a hash from another file
by tospo (Hermit) on Oct 14, 2011 at 13:45 UTC
    OK, you got some solutions for getting this to work but please note that your approach just isn't a good idea in the first place (sorry).
    YOu currently populate the hash by actually typing stuff into a file that you then save as a perl module. However, it can very easily be generated by the code using loops as I mentioned earlier in the other thread you are refering to. It sort of defeats the purpose of scripting if you then end up typing all the combinations of categories and sub-categories by hand into a file (i.e. hardcoding them).
    Do yourself a favour and have a go at using loops to let the code generate all these tedious combinations of things and you will begin to see "The Light"(Tm) :-)
    Good luck!
      Yes and no. If the logic gets torturous I find it better just to create a table even if it is almost wholly repetitive. If it is large (or small), if most entries are the same or even empty. Perl doesnít mind tedious.

      As soon as there are nots, ors and ands and they start nesting and cascading all over the place Iím certain, nay, guaranteed, to get it wrong. I find it hard to write, the bugs hard to find and they are most often the sort of bugs that appear six months down the line. Did I mention my boolean algebra is weak?

      If it is appropriate and I can fit the logic into a table I get on much better, it is all reduced to a lookup. If it is not appropriate then yours is the way to go. What is appropriate, of course, is a matter of taste. :-)

        well, Perl doesn't mind tedious but you might and you are likely to get nice little errors in the long table that will be difficult to find. Why risk it? It's like wanting to print out a multiplication table by hardcoding all the results instead of calculating them. Yes, you can do it and Perl doesn't mind but it would defeat the purpose, wouldn't it?
      Thank you tospo!
      You mean the parameter could by saved in csv-file which could look like:
      shorthand;title;indicator;name;yskala;legend THIS;This Evaluation;01;First;[:100];bottom THIS;This Evaluation;02;Second;[:100];bottom THIS;This Evaluation;15;Fifteen;[0:];top
      and be read into the hash?
      Of course it would simplify the build-up. I will try to accomplish this, though I have read some threads on this subject, as converting a text into a hash datastructure or Building an arbitrary-depth, multi-level hash and did not see "The Light" yet :-)
      I'll try.

      Definitely OT but I could not stop myself: I made a funny typo today: "sue strict; sue warnings;". Well, Perl did not do it :-)

        not really. The crucial point is that the entire content of that csv file can be auto-generated in a few linew unless I'm missing something important here.
        Take this as a simple example:
        my @combinations = ( "1,1","1,2","2,1","2,2")
        my @combinations; for my $i (1..2){ for my $j (1..2) { push @combinations, "$i,$j"; } }
        I'm generating an array in this case with strings that are combinations of elements, in this case simply the number 1 and 2 but you can easily substitute that with "THIS", "THAT" and "This Evaluation", "That Evalutation" etc, which is what you have (if I remember your other thread correctly).
        In the first example, I harcode the array by typing all the combinations in my script. I could also save them to a file in csv format, as dump of the array (using Data::Dumper), or whatever format you can think of - the problem remains: I'm hardcoding all possible but entirely predictabe and therefore scriptable combinations of elements.
        In the second example, I generate all possible combinations on the fly. Again, if I remember correctly, you only need to do that to generate a file name. For that reason you don't even need to save the data to an array or a hash - you just generate the string as a combination of all the elements and then use that to open your file - job done.
        Of course, in my simple example, the second version is longer than the first, but you can easily see how that changes quickly with more elements and more combinations.
        So, if the data can be generated on the fly, do it - that's the whole point of scripting. If not, i.e. sometimes you want the scale for a "THIS" plot to be "0:" and sometimes ":100" and tehre is no way to predict it, then you have to resort to a config file (for which you can use a number of options, such as YAML or simple config format - have a look around on the CPAN).
        Hope this helps.
        I used to know a girl called sue strict. shudders
Re: How to access a hash from another file
by Anonymous Monk on Oct 14, 2011 at 12:38 UTC
Re: How to access a hash from another file
by vagabonding electron (Curate) on Oct 14, 2011 at 12:58 UTC
    Thank you All,
    it works now with the solutions of wfsp and JavaFan.
    choroba I do have YAML in my ActivePerl distribution and I even made an pitiful attempt with it, but it seems that up by now it exceeds my knowledge in Perl.
    Thank you Anonymous Monk for the links to the tutorials too.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://931485]
Approved by herveus
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2023-12-11 03:12 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (41 votes). Check out past polls.