I have done some digging through perldoc and the O’Reilly books but haven’t found any way to do this. Am I relegated to using something like Readonly?
UPDATE:
I have nothing against Readonly. I just wanted to be able to do something like PHP’s constant().
Example if Perl had constant():
use constant {
FIELD_EXAMPLE_O => 345,
FIELD_EXAMPLE_L => 25
};
my $var = 'EXAMPLE';
my $c = 'FIELD_' . $var . '_L';
my $value = constant($c);
# $value is 25
If Readonly is the best way to do it, then I shall use it.
What’s wrong with
Readonly?If it’s too slow, you can supplement it with
Readonly:XS. But if you don’t likeReadonly, there’s always the olderconstant.Just remember
If you want to create multiple constants in one statement, you need to pass a hash reference.
From Your Example:
Judging from your example, there’s no reason why a readonly hash couldn’t do the same thing.
Then you could use it anywhere you’d want to cobble the constant:
OR you could do it this way:
And call it this way:
You can get a lot of mileage about not trying to make a language do what it has better support for doing in another way.
Cognate to PHP
constantHowever, if you want to do it that way, then my suggestion is that the following sub is a way of doing the same ( and avoids an
eval):You’d call it like this:
One last thing, is that you could even put a test in front to make sure that it is only a all cap string: