Perl: "creating" variable names

Status
Not open for further replies.

CanSpice

Ars Tribunus Angusticlavius
7,147
I used to know how to do this, but now I've completely forgotten. How do you "create" a variable name out of another variable?

I've created some hashes named %onehdr, %twohdr, %threehdr. I have a variable called $dynamic that can be "one", "two", or "three". Now, how do I go about dynamically referring to the hashes?

I've tried something like this:

code:<HR>
Code:
${$dynamic}hdr{'foo'}

... but that doesn't work AT ALL. Any ideas?
 

petrov

Ars Tribunus Militum
2,714
you need to make sure that strict reference checking is disabled, and then you can do something like this:

code:<HR>
Code:
no strict 'refs';  # disable strict reference checking

# either of these will work
${$dynamic . 'hdr'}{'foo'} = 'bar';
%tmp = %{ $dynamic . 'hdr'};

depending on your opinion of perl, the capability to do this is a bug or feature.

--sam
 
Status
Not open for further replies.