Re: Using a scalar as an array name
by PodMaster (Abbot) on May 15, 2005 at 21:49 UTC
|
A search for use variable as variable , will yield in the result set Why it's stupid to `use a variable as a variable name', and once more You just have to read (and I gotta tell you about)
Why it's stupid to `use a variable as a variable name' Part
1
2
3
:)
You really should read those three. It explains how to do what you're attempting, and why it's a bad idea.
| MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!" | | I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README). | | ** The third rule of perl club is a statement of fact: pod is sexy. |
| [reply] |
|
|
I knew it! Someone was gonna say that :)
Thanks for the reply, but let's say I still want to do it? For learning purposes, shall we say?
| [reply] |
Re: Using a scalar as an array name
by Animator (Hermit) on May 15, 2005 at 21:46 UTC
|
No, what you are using is a soft-reference, and 'use strict refs' only allows hard references.
So you have two options: either: $MyScalar = \@MyArray (a hard-reference) or 'no strict qw/refs/'.
For more information about references you can consult the perlreftut POD and/or the perlref POD
| [reply] [d/l] |
|
|
Mmmm.... thanks for the reply, but here's the issue: @MyArray already exists, and I can't very well modify $MyScalar to be a hard reference to @MyArray. $MyScalar gets its values assigned from CGI. If $MyScalar holds a string that corresponds to an existing array @MyArray I want to be able to access it.
Oh, yeah, and I know about taint mode and all that... getting stuff from CGI and dereferencing it like that is dangerous... but I'd bungee jump too if I get the chance, and it's not my server anyway :)
| [reply] |
|
|
my %array_references = (myarray => \@MyArray, second_array => \@anothe
+r-array);
And somewhere else in your script:
my $reference = $array_references{lc $input};
print $reference->[0]; # (for example)
This gives you a higher control on what the world can access, and allows you to run the script under 'use strict' | [reply] [d/l] [select] |
|
|
my %vars = (
MyArray => \@MyArray,
HisArray => \@HisArray,
OtherArray => \@TheirArray,
);
my $ref;
unless( exists $vars{ $MyScalar } ) {
die "Error: Don't know about '$MyScalar'\n";
} else {
$ref = $vars{ $MyScalar };
}
## Use $ref . . .
| [reply] [d/l] |
Re: Using a scalar as an array name
by davidrw (Prior) on May 15, 2005 at 22:26 UTC
|
I'd strongly advise heeding the above advice to avoid using a variable as a variable name, but eval might get you the hack you're looking for:
my @MyArray = ( 1, 2, 3, 4 );
my $varname = 'MyArray';
my $x = eval '$' . $varname . '[0]'; # THIS IS BAD!!!!!
print $x; # 1
Update: I should re-emphasize that the above is BAD as in "don't cross the streams" -- I only posted it because it technically works and the OP insisted on knowing any kind of way to accomplish it. | [reply] [d/l] [select] |
|
|
Warning: doing what the previous reply says is really insecure and should not be used!
There is only one thing I can say about that: it is bad, even worse then not using 'use strict'
What will happen is that it will evaluate $varname as perl-code(!), not just as a string.
And Since $varname is really coming from a cgi script, meaning user input, it allows anyone to run commands! secure? I don't think so.
What do you think happens when $varname = qq(system("echo running the echo command");); for example ?
| [reply] [d/l] |
|
|
Okay, you are all right. I'm using a hash. See - I've learned. Happens every time I come here. :)
Thank you all.
| [reply] |