spurperl has asked for the wisdom of the Perl Monks concerning the following question:
There's an interesting issue I ran into, not for the first time:
My module contains some hash that maps data. It is constant and is changed only by the programmer, for example:
my %opcode_map = ( 'LDA' => [8, 0], 'STA' => [14, 1], ... ... );
The 'my' makes this map private to the module, as it should be (only functions from the module use it, it's never exported). However, when a table contains many duplications that can be generated automatically, I'm not sure what is the best way to generate the entries.
For instance, there are keys 'LD1', 'LD2', ... up to 6, with the opcode also having a pattern which can be easily generated. So, instead, I place 'LDi" and then a loop replaces each i by 1..6 and adds all the relevant entries into the map.
In the past, I just slapped the code "in the wild" in the module's scope. It runs when the module is "use"-d, and that's it. However, it doesn't look "clean" to me, and I wonder whether ther are better ways ?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: code that runs at module loading
by Zaxo (Archbishop) on Dec 07, 2004 at 08:10 UTC | |
|
Re: code that runs at module loading
by eyepopslikeamosquito (Archbishop) on Dec 07, 2004 at 08:49 UTC | |
|
Re: code that runs at module loading
by conrad (Beadle) on Dec 07, 2004 at 10:57 UTC | |
|
Re: code that runs at module loading
by sgifford (Prior) on Dec 07, 2004 at 17:22 UTC | |
by rrwo (Friar) on Dec 07, 2004 at 20:34 UTC | |
by sgifford (Prior) on Dec 07, 2004 at 21:06 UTC |