in reply to Crazy constant question...

Sticking an evaled use constant in a BEGIN block would seem to meet your requirement, and avoid those nasty typeglobs:
#!/usr/bin/perl use strict; use warnings; my @num_to_name; BEGIN { @num_to_name = qw(FOO BAR); eval "use constant $num_to_name[$_] => $_" for 0 .. $#num_to_name; } print "FOO-->", FOO(), "-->", $num_to_name[FOO];

I've assumed your names are necessarily 1:1 and adjacent integers starting at 0 - this is what your code seems to do. You could do something with hashes instead for more flexibility.

Replies are listed 'Best First'.
Re^2: Crazy constant question...
by ikegami (Patriarch) on Dec 15, 2010 at 21:08 UTC

    Avoiding eval:

    my @num_to_name; BEGIN { @num_to_name = qw(FOO BAR); require constant; constant->import( $num_to_name[$_] => $_ ) for 0 .. $#num_to_name; }

    Avoiding eval and multiple calls to import:

    my @num_to_name; BEGIN { @num_to_name = qw(FOO BAR); require constant; constant->import({ map { $num_to_name[$_] => $_ } 0..$#num_to_name + }); }