in reply to code that runs at module loading

One technique I use sometimes is to write another very small program to generate Perl code to create the constant hash, then paste the output of that program into your main program. Something like:
print "my %opcode_map = (\n"; print "LD$_ => [".join(',',$_*3,$_*5,$_*7)."],\n" foreach (1,2,3); print ")\n";

Also, note that you should use parentheses not squiggly brackets to set up your hash, or else you may not get what you expect; use warnings will warn you about that.

Replies are listed 'Best First'.
Re^2: code that runs at module loading
by rrwo (Friar) on Dec 07, 2004 at 20:34 UTC

    Having the script that uses the constant generate the code at compile time (in a BEGIN block) is one of the wonderful features of Perl that distinguishes it from other languages. Why not make use of it?

      I was just throwing out a technique I use sometimes to see if the OP might find it useful; it's not necessarily the best strategy here. It's useful if the constants take a long time to generate, or if they're actual use constant declarations, which Perl can optimize away at compile-time if it knows them beforehand.