You mean something like this?

no strict 'refs'; $red = "#ff0000"; $color = ${ "red" };
Yes, it's possible :) and sometimes even useful, though in general you're better off reading (and understanding) perlref and using symbolic references, as the above are called, only when you really need them.

By the way, instead of "red" in ${ "red" } you can put any expression, which will be evaluated in scalar context to produce the name of the variable to use. Note that you can only access vars that are in the symbol table, so that if I did my $red = ...;, I would not have access to it using this technique.

Update: If this is your first go, maybe perlreftut would be an easier read. You cannot achieve deep understanding of Perl without reading the former, though.


In reply to Re: Variable name containing a variable by gaal
in thread Variable name containing a variable by jets80

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.