Greetings Monks!
This question is probably more trouble than it's worth, but it's been bugging me, so here goes:
I can create constants like so:
but if I want a way to convert the number back into a string, I need to do something that defines the same relationships again, likeuse constant { FOO => 1, BAR => 2 };
which allows me to do things like:my @num_to_name = qw(FOO BAR);
my $thing = FOO; print "Thing = ".$num_to_name[$thing]."\n";
My problem is, I hate that I have to maintain my constant declarations and my @num_to_name list separately. If I was doing this with a large number of constants, I'd screw it up for sure.
I actually made a thing that sort of does what I want, though It's cludge-tastic and probably all wrong and unnecessarily stupid:
my @num_to_name; BEGIN { @num_to_name = qw(FOO BAR); for my $number ( 0 .. $#things) { *blah = eval "*My::Package::".$things[$number]; *blah = sub { $number }; } }
With that monster, $num_to_name[FOO] eq 'FOO' no matter what. It loses the inlining of use constant, which is part of what's lame about it.
Is there a way to do this that sucks less?
Thank you for your indulgence
--Pileofrogs
In reply to Crazy constant question... by pileofrogs
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |