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.