Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Using Exporter with a complex hash

by glemley8 (Acolyte)
on Oct 23, 2012 at 19:58 UTC ( #1000517=perlquestion: print w/replies, xml ) Need Help??

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

I'm using a pretty complex hash table that needs to be in a central place, while a few scripts refer to it. Here is an example of the structure:

my %hashtable = ( STATION1 => { code => 'blue', parameters => ['a','b','c','d'], range => [4,7,6,9] }, STATION2 => { code => 'green', parameters => ['b','c','d','e'], range => [6,2,5,7] }, STATION3 => { code => 'red', parameters => ['a','c','d','e'], range => [8,2,1,5] }, );

"STATIONx" is determined by user input when executing the initial script. The scripts ratchet through each parameter and their parallel array values (value/param 0, 1, 2, etc.).

By using Exporter and placing the hash table in a module (method posted here, I was able to grab the "code" key, but could not figure out the syntax to grab param/range x. Can anyone lead me in the right direction?

Let me know if you need further info about the goals of this script (and please be easy; I'm learning).

Replies are listed 'Best First'.
Re: Using Exporter with a complex hash
by tobyink (Canon) on Oct 23, 2012 at 20:29 UTC

    Lexical (my) variables cannot be exported. Only package (our) variables can. But please do heed the following warning found in the Exporter documentation:

    "There's one more item to add to this list. Do not export variable names. Just because "Exporter" lets you do that, it does not mean you should.

    @EXPORT_OK = qw( $svar @avar %hvar ); # DON'T!

    "Exporting variables is not a good idea. They can change under the hood, provoking horrible effects at-a-distance, that are too hard to track and to fix. Trust me: they are not worth it.

    "To provide the capability to set/get class-wide settings, it is best instead to provide accessors as subroutines or class methods instead."

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Using Exporter with a complex hash
by NetWallah (Canon) on Oct 23, 2012 at 21:46 UTC
    If you are using this syntax (from the stackoverflow comment):
    my %config = getconfig(); print $config{ somthingelse };
    Then you can print the params by:
    print join(",", @{ $config{STATION2}{parameters} });

                 "By three methods we may learn wisdom: First, by reflection, which is noblest; Second, by imitation, which is easiest; and third by experience, which is the bitterest."           -Confucius

Re: Using Exporter with a complex hash
by Don Coyote (Friar) on Oct 23, 2012 at 22:49 UTC

    As you are accessing the 'code' key, this is not an exporter issue. But im really glad the our my difference was pointed out here as I just wrote my first object tonight!!

    NetWallah has given you a correct way of accessing the array value from the hash of the hash, the hashtable name is a bit misleading. The data is just a complex hash.

    you have to access the values depending on the context of the value. If you want the whole array, access in the way NetWallah has shown, and in a similar fashion you can also access the subscripts. Put back into the code of the op...

    print qq(\n), @{ $hash{'STATION1'}->{'parameters'} } ; print qq(\n), $hash{'STATION1'}->{'parameters'}[0] ; ------- abcd a

    I tried to export through my new object script into my calling script but did not have a lot of success, I just pasted into my calling script to test this. Oh yeah, works if I update lexical my to package our. nice!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2021-12-05 22:08 GMT
Find Nodes?
    Voting Booth?
    R or B?

    Results (31 votes). Check out past polls.