in reply to Using a scalar as an array name

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

Replies are listed 'Best First'.
Re^2: Using a scalar as an array name
by rvosa (Curate) on May 15, 2005 at 21:56 UTC
    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 :)

      Then why don't you use a hash too? which holds the keyword and the associated array? So you can limit the access?

      Something like:

      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'

      Then you want to use a hash as a dispatch table to retrieve a proper reference.

      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 . . .