I should use a hash instead, but humor me, would you ?
Well, just this once :-)
Actually, it works for me on the command line if not using strictures and not declaring the variables so they default to being package variables.
$ perl -le ' > $foo = q{foo}; > $bar = q{f}; > print ${$bar . q{oo}};' foo $
Enabling strictures and using lexical varables throws a "strict refs" error
$ perl -Mstrict -wle ' > my $foo = q{foo}; > my $bar = q{f}; > print ${$bar . q{oo}};' Can't use string ("foo") as a SCALAR ref while "strict refs" in use at + -e line 4. $
Turning the stricture off inside the print statement throws an "uninitialsed value" warning because you can't have soft references with lexical variables.
$ perl -Mstrict -wle ' > my $foo = q{foo}; > my $bar = q{f}; > print do {no strict q{refs}; ${$bar . q{oo}} };' Use of uninitialized value in print at -e line 4. $
Declaring the variables as package variables with our gets things working again.
$ perl -Mstrict -wle ' > our $foo = q{foo}; > our $bar = q{f}; > print do {no strict q{refs}; ${$bar . q{oo}} };' foo $
But having said all of that, you really should be using a hash and should avoid soft references like the plague if at all possible.
Cheers,
JohnGG
In reply to Re: Variable name
by johngg
in thread Variable name
by nsteiner
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |