FAQ 7.29 How can I use a variable as a variable name?

FAQ 7.29 How can I use a variable as a variable name?

am 30.03.2008 15:03:04 von PerlFAQ Server

This is an excerpt from the latest version perlfaq7.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

------------------------------------------------------------ --------

7.29: How can I use a variable as a variable name?

Beginners often think they want to have a variable contain the name of a
variable.

$fred = 23;
$varname = "fred";
++$$varname; # $fred now 24

This works *sometimes*, but it is a very bad idea for two reasons.

The first reason is that this technique *only works on global
variables*. That means that if $fred is a lexical variable created with
my() in the above example, the code wouldn't work at all: you'd
accidentally access the global and skip right over the private lexical
altogether. Global variables are bad because they can easily collide
accidentally and in general make for non-scalable and confusing code.

Symbolic references are forbidden under the "use strict" pragma. They
are not true references and consequently are not reference counted or
garbage collected.

The other reason why using a variable to hold the name of another
variable is a bad idea is that the question often stems from a lack of
understanding of Perl data structures, particularly hashes. By using
symbolic references, you are just using the package's symbol-table hash
(like %main::) instead of a user-defined hash. The solution is to use
your own hash or a real reference instead.

$USER_VARS{"fred"} = 23;
$varname = "fred";
$USER_VARS{$varname}++; # not $$varname++

There we're using the %USER_VARS hash instead of symbolic references.
Sometimes this comes up in reading strings from the user with variable
references and wanting to expand them to the values of your perl
program's variables. This is also a bad idea because it conflates the
program-addressable namespace and the user-addressable one. Instead of
reading a string and expanding it to the actual contents of your
program's own variables:

$str = 'this has a $fred and $barney in it';
$str =~ s/(\$\w+)/$1/eeg; # need double eval

it would be better to keep a hash around like %USER_VARS and have
variable references actually refer to entries in that hash:

$str =~ s/\$(\w+)/$USER_VARS{$1}/g; # no /e here at all

That's faster, cleaner, and safer than the previous approach. Of course,
you don't need to use a dollar sign. You could use your own scheme to
make it less confusing, like bracketed percent symbols, etc.

$str = 'this has a %fred% and %barney% in it';
$str =~ s/%(\w+)%/$USER_VARS{$1}/g; # no /e here at all

Another reason that folks sometimes think they want a variable to
contain the name of a variable is because they don't know how to build
proper data structures using hashes. For example, let's say they wanted
two hashes in their program: %fred and %barney, and that they wanted to
use another scalar variable to refer to those by name.

$name = "fred";
$$name{WIFE} = "wilma"; # set %fred

$name = "barney";
$$name{WIFE} = "betty"; # set %barney

This is still a symbolic reference, and is still saddled with the
problems enumerated above. It would be far better to write:

$folks{"fred"}{WIFE} = "wilma";
$folks{"barney"}{WIFE} = "betty";

And just use a multilevel hash to start with.

The only times that you absolutely *must* use symbolic references are
when you really must refer to the symbol table. This may be because it's
something that can't take a real reference to, such as a format name.
Doing so may also be important for method calls, since these always go
through the symbol table for resolution.

In those cases, you would turn off "strict 'refs'" temporarily so you
can play around with the symbol table. For example:

@colors = qw(red blue green yellow orange purple violet);
for my $name (@colors) {
no strict 'refs'; # renege for the block
*$name = sub { "@_" };
}

All those functions (red(), blue(), green(), etc.) appear to be
separate, but the real code in the closure actually was compiled only
once.

So, sometimes you might want to use symbolic references to directly
manipulate the symbol table. This doesn't matter for formats, handles,
and subroutines, because they are always global--you can't use my() on
them. For scalars, arrays, and hashes, though--and usually for
subroutines-- you probably only want to use hard references.



------------------------------------------------------------ --------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.