FAQ 7.28: How can I use a variable as a variable name?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

This message is one of several periodic postings to comp.lang.perl.misc
intended to make it easier for perl programmers to find answers to
common questions. The core of this message represents an excerpt
from the documentation provided with Perl.


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

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

        $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 = 23;
        $varname = "fred";
        $USER_VARS++;  # 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/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/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 = "wilma";     # set %fred

        $name = "barney";
        $$name = "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   = "wilma";
        $folks = "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

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

    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.


Documents such as this have been called "Answers to Frequently
Asked Questions" or FAQ for short.  They represent an important
part of the Usenet tradition.  They serve to reduce the volume of
redundant traffic on a news group by providing quality answers to
questions that keep coming up.

If you are some how irritated by seeing these postings you are free
to ignore them or add the sender to your killfile.  If you find
errors or other problems with these postings please send corrections
or comments to the posting email address or to the maintainers as
directed in the perlfaq manual page.

Note that the FAQ text posted by this server may have been modified
from that distributed in the stable Perl release.  It may have been
edited to reflect the additions, changes and corrections provided
by respondents, reviewers, and critics to previous postings of
these FAQ. Complete text of these FAQ are available on request.

The perlfaq manual page contains the following copyright notice.


    Copyright (c) 1997-2002 Tom Christiansen and Nathan
    Torkington, and other contributors as noted. All rights

This posting is provided in the hope that it will be useful but
does not represent a commitment or contract of any kind on the part
of the contributers, authors or their agents.

Site Timeline