Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Name of constant in variable

Status
Not open for further replies.

fagga

Programmer
Jun 26, 2002
28
DE
Good day,

is there any way to store the name of a constant (not the value) in a variable and access the value of this constant through the variable?
For example, if I had a constant named FOO.
Code:
my $var = 'FOO';  # FOO contains "bar"
print $var;       # should print "bar", prints "FOO"

I know, it would work if I'd just remove the quotes, but I just get the name of the constant from a function, not the value.

Hope you can understand me. And thanks in advance.
 
Well, if I try this, it print's 'bar'. But in your example 'bar' is the name of the constant and I need the value.

Code:
constant ANSWER => 42;          # some constant
$name_of_constant = 'ANSWER';   # $name_of_constant contains 'ANSWER', not '42'
print magic($name_of_constant); # prints magically '42'

Now I just need to know what this strange magic function looks like.
 
if you are wanting more examples google perl constant. as it stands this bit of code won't execute. sorry I cant be more help


 
Constants in perl are just subroutines that return the value. So you probably are wanting to do a soft dereference.

Code:
use constant FOO => 'bar';
my $con = 'FOO';
print &$con;  # gives 'bar'

--jim
 
Yes, this works fantastic. Thank you all very much for your help. I'm highly satisfied.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top