develooper Front page | perl.beginners | Postings from April 2003

Constant name from value

From:
wiggins
Date:
April 24, 2003 13:06
Subject:
Constant name from value
Message ID:
200304242006.h3OK6Rl06373@crows.siteprotect.com
Given a namespace that contains a large number of constants all starting with the same name, and all set to a list of two values where the first value is a number, for instance:

package MyConstants;

use constant EXC_ONE => ('1000', 'One thousand');
use constant EXC_TWO => ('2000', 'Two thousand');

Is it possible to reverse index to retrieve the constant name?  So given a value '1000', for instance, is it possible to somehow determine that it is associated with 'EXC_ONE' the name?

I have tried stepping through the namespace, etc. and naturally can get the correct name space entry (which is how I am exporting the list, namespace pollution, etc. I know), to which I get something like:

EXC_TWO: *MyConstants::EXC_TWO

But I can't figure out how to go from the *MyConstants::EXC_TWO to the first value of the list...if it can be done. Also tried some trickery with 'eval' but that led nowhere.

If more info is needed let me know and I can provide it....

http://danconia.org



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About