in reply to Using a scalar as an array name

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.

Replies are listed 'Best First'.
Re^2: Using a scalar as an array name
by Animator (Hermit) on May 15, 2005 at 22:34 UTC

    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 ?

      Okay, you are all right. I'm using a hash. See - I've learned. Happens every time I come here. :)

      Thank you all.