How can I use the value of a variable as a variable name in Perl?
Asked Answered
T

3

1

If I have a variable, $bar, which is equal to string "foo" and $foo is equal to 0xdead, how can I get $foo's value while I only have the the string for the variable name?

Essentially, I want to do a kind of pointer indirection on the global namespace or a hash lookup on the global namespace.

The following didn't work:

perl -e 'my $foo=0xdead; my $bar ="foo"; print ${$bar}."\n";' 

It only prints the newline.

Tonedeaf answered 22/2, 2010 at 21:51 Comment(2)
Before you go down that road, make sure you've read all of this: perl.plover.com/varvarname.html.Sikh
See also #1550185 ... Oh, and, you should not want to do this.Baird
B
7

This trick works only with global variables (symbolic references seek the symbol table of the current package), i. e.

perl -e '$foo=0xdead; my $bar ="foo"; print ${$bar}."\n";' 

If you want to catch lexicals, you'll have to use eval ""

perl -e 'my $foo=0xdead; my $bar ="foo"; print eval("\$$bar"),"\n";' 

But using eval "" without purpose is considered bad style in Perl, as well as using global variables. Consider using real references (if you can).

Befog answered 22/2, 2010 at 21:55 Comment(0)
P
4

There are very very very preciously few instances in Perl where you must use symbolic references. Avoiding symbolic references in all other instances is not about style. It is about being a smart programmer. As mjd explains in Why it's stupid to "use a variable as a variable name":

The real root of the problem code is: It's fragile. You're mingling unlike things when you do this. And if two of those unlike things happen to have the same name, they'll collide and you'll get the wrong answer. So you end up having a whole long list of names which you have to be careful not to reuse, and if you screw up, you get a very bizarre error. This is precisely the problem that namespaces were invented to solve, and that's just what a hash is: A portable namespace.

See also Part 2 and Part 3.

Perea answered 22/2, 2010 at 23:10 Comment(3)
My goal is to iterate over a bunch of variables, and if any of the variables are set to a certain value, I want to report that variable name. Thus I need to variable value and the variable name.Tonedeaf
@Ross Rogers Are those variables somehow related? Presumably. Then, use a hash. That's what the symbol table is: A hash.Baird
I'm interfacing with an old, custom option parser that stores its values in the global namespace. If I had my way, we wouldn't use our own custom option parser, but use parseopts instead.Tonedeaf
I
0

Without my and with $$bar works for me:

$ perl -e '$foo=0xdead;$bar ="foo"; print $$bar."\n";'
57005

You can find out more about using a variable as a variable name in the Perl FAQ List.

Isfahan answered 22/2, 2010 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.