A brief summary: First, don't use string eval for this. It harms performance, and can mask problems with your code.
Second, the main question is why you need to do this. If you need a key-value lookup facility, Perl already has a data structure for that, and you should use it.
If you only need to do this in one place, invoking the constants created with your use constant statement as functions by constructing their names is OK. That's what Hynek -Pichi- Vychodil's answer is doing.
If you need to do this in multiple places in your code, you'll be better off mediating the lookups so your code is not littered with no strict 'refs'. That's what pilcrow's answer gives you. The check for the definedness of the constant subroutine hurts performance, but is necessary if you want the exception to tell you what part of your code tried to look up a non-existent value.
To me, it looks like Const::Fast is more appropriate for your situation. It allows you to collect related constants in a single namespace that is not your package namespace, look up and interpolate those constants using simple Perl constructs etc.
use Const::Fast;
const my %STATS_AXLE => (
SPOT_OK => 0,
SPOT_ERROR => 1,
SPOT_SKIPPED => 2,
FORWARD_OK => 3,
FORWARD_ERROR => 4,
FORWARD_SKIPPED => 5,
);
Then you can do say $STATS_AXLE{SPOT_ERROR} or
say $STATS_AXLE{ "${l_deal_type}_${l_status}" };
or
say $STATS_AXLE{ "$_[0]_$_[1]" };
is your DoStuff routine.
This will croak if the key does not exist in %STATS_AXLE.
For an excellent comparison of CPAN modules for defining constants, please see Neil Bowers' excellent review. His recommendation, which I agree with, is:
If you want array or hash constants, or immutable rich data structures, use Const::Fast. It's a close race between that and Attribute::Constant, but Const::Fast seems maturer, and has had more
releases.
PS: Note that sub DoStuff() declares DoStuff as not taking any arguments. Just don't use Perl's prototypes. They don't do what most people expect them to do. Do:
sub DoStuff {
....
}
evalto get dynamic constants sounds like a really, really bad idea. I believe in 99.9999% of the time I would just use a regular variable instead. Never quite got the point of constants, its a variable that you should not change, virtually indistinguishable from a variable that you never change.constantmight have overConst::Fast.