pileofrogs has asked for the wisdom of the Perl Monks concerning the following question:
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Crazy constant question...
by SuicideJunkie (Vicar) on Dec 15, 2010 at 20:57 UTC | |
Re: Crazy constant question...
by kennethk (Abbot) on Dec 15, 2010 at 21:01 UTC | |
by ikegami (Patriarch) on Dec 15, 2010 at 21:08 UTC | |
Re: Crazy constant question...
by johna (Monk) on Dec 15, 2010 at 21:22 UTC | |
Re: Crazy constant question...
by moritz (Cardinal) on Dec 15, 2010 at 21:23 UTC | |
Re: Crazy constant question...
by SilasTheMonk (Chaplain) on Dec 15, 2010 at 22:16 UTC | |
Re: Crazy constant question...
by thargas (Deacon) on Dec 16, 2010 at 14:04 UTC | |
Re: Crazy constant question...
by AnomalousMonk (Archbishop) on Dec 16, 2010 at 14:58 UTC | |
Re: Crazy constant question...
by aartist (Pilgrim) on Dec 16, 2010 at 16:40 UTC |