Symbolic references are absolutely the wrong way to go about the problem you are trying to solve, as are global variables. Read perlintro and you will learn about hashes (which you are already using but seem to not be using in all the places you should). Perl's symbol tables internally are implemented using hashes. If it works for Perl, it's certainly going to be good enough for you too. But even though it's a terrible use case, referring to variables using a variable is possible:

$red = 1; $blue = 2; $green = 3; foreach $color (qw(red blue green)) { print "$color: $$color\n"; }

But this is dangerous, bug prone, broken by design. Anywhere you feel you need to name a variable programatically, you need either a hash, an array, or a hard reference, which you can read about in perlref and perlreftut. Hashes and arrays are discussed in perlintro and perldata. Putting it all together is discussed in perldsc, perllol.

Someone already linked to MJD's venerable trilogy of Usenet posts about why it's stupid to use a variable as a variable name. He was right 20 years ago when he posted that, and is still right. The road you are going down leads to unsolvable bugs that can only be fixed by complete rewrite.

perlsub will discuss lexical variables. strict and warnings will discuss how to put the brakes on some forms of truly awful design decisions.

Please take a few hours now learning to do it right rather than succumbing to the allure of getting it done quickly with limited understanding. Doing it right will be better for you in the longrun.

There are legitimate uses for symbolic references. Those uses are very limited, and very narrow where they should be applied. Those uses should live buried deep in utility modules with nice clean user interfaces. Nobody should ever see them. If symbolic refs didn't even exist in Perl you, the happy programmer, would almost never even notice their absence.


Dave


In reply to Re: Calling a variable value as a variable by davido
in thread Calling a variable value as a variable by echo5

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.