I have a set of constants declared in Perl:
use constant C1 => 111;
use constant C2 => 222;
..
use constant C9 => 999;
my $which_constant = "C2";
How do I construct a Perl expression which, based on $which_constant
, derives the value of a constant named with the value of this variable - e.g. "222".
Please note that I can not change any of the conditions above - they are a simplification of a real scenario: I have a module (which I have no control over) from which these constants are imported. The `name of one of the constants is supplied by the user from command line. I need to access the appropriate constants' value.
I've been beating my head against the wall (mostly around all sorts of weird glob constructs) but none of them work.
P.S. If the solution accesses the constants inside their native module - say My::Constants::C2
(without needing to import them), even better, but not necessary - I can import the correct constants into main::
easily using My::Constants->import($which_constant)
. and yes, to top it off, te constants are NOT exported by default thus needing the explicit import() call.
Some of the things I tried:
main::$which_constant
- syntax error
main::${which_constant}
- syntax error
${*$which_constant}
- returns empty value
*$which_constant
- returns "*main::C2"
${*${*which_constant}}
- empty
Constants defined by constant.pm
are just subroutines. You can use method invocation syntax if you have the name of the constant in a string:
#!/usr/bin/perl -l
use strict; use warnings;
use constant C1 => 111;
use constant C2 => 222;
print __PACKAGE__->$_ for qw( C1 C2 );
# or print main->$_ for qw( C1 C2 );
This way, if you try to use a constant which is not defined, you will get an error.
Perl "constants" are actually subroutines that return a constant value. The perl compiler is able to replace them with the appropriate value at compile time. However, since you want to get the value based on a runtime name lookup, you should do:
&{$which_constant}();
(And of course you need no strict 'refs'
somewhere.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With