TecoDaN has asked for the wisdom of the Perl Monks concerning the following question:

wh00ps, ignore the last messy post. ok, lets have a look at this bit of code first:
---

$aword = "foo";
$containmentvar = '$aword';
print $containmentvar;

---
What I want the code to do is print the word foo from the scalar variable $containmentvar that is a character string containing the variable name $aword. The above code won't do that. How could I make it possible with a code similar to the above?

  • Comment on substitution of variable within a variable

Replies are listed 'Best First'.
RE (tilly) 1: substitution of variable within a variable
by tilly (Archbishop) on Sep 24, 2000 at 05:23 UTC
    What you are asking for is a very, very bad idea. It is called a "symbolic ref" and you can do it very easily in Perl, but you shouldn't. For details on how to do it, and how to stop yourself from doing it by accident, see the strict pragma. Also see perlref, which explains how to do hard references, the much, much safer way to get the job done.
      Another good pointer would be this entry in perlfaq7, which discusses the faults of symbolic refs.

      update: another (better) discussion of the issue can be found here (by Mark-Jason Dominus).
(jeffa) Re: substitution of variable within a variable
by jeffa (Bishop) on Sep 24, 2000 at 05:10 UTC
    You got your single and double quotes backwards.
    Looks like you need to say:
    $aword = 'foo'; # use single quotes instead of double $containmentvar = "$aword"; # use double quotes instead of single
    Variables between double quotes will be interpolated to their values, but between single quotes they will be taken literally.

    Hope this helps,
    Jeff

    UPDATE:
    Hmm, when I first read your post, I thought you were wanting symbolic references - I have to agree with tilly on this one, and do read ar0n's link that he provided, but here you go anyways:

    $aword = 'foo'; $containmentvar = 'aword'; # no dollar sign here $$containmentvar = 'bar'; # foo or bar? print "$aword\n";
    Remember, you can't use strict with this code, that should be reason enough to find another solution, but if you aren't coding heart monitoring devices you should be ok.

    Now that I have handed a loaded gun to somebody, I am going to find a lawyer.

update: substitution of variable within a variable
by TecoDaN (Initiate) on Sep 24, 2000 at 05:22 UTC
    hmm, i should make the problem a bit more specific,

    a variable name, $aword, is stored in an array within a character string, to access it i did:

    foreach $containmentvar (@newvarwords) {
    print $containmentvar;
    }

    ---
    I wanted to print the word stored in $aword, yes, i should have used single quotes for that, but i still dont know how to access it by doing a similar code to the above.

Re: substitution of variable within a variable
by TecoDaN (Initiate) on Sep 24, 2000 at 07:04 UTC
    aye, thanks for those links.
    I remembered about reading somewhere that sym refs are bad, so i think i can figure out how to do it with hard references now that i know what i am actually looking for.

    Thanks for all your help, I mite actually get somewhere in life, lol.

      Liked this thread, learned that symbolic refs are Evil...
      Note from former PHP pagan, forgive me brothers :-) PHP has only symbolic refs AFAIK,
      now I see this is definitely Bad Thing. So, another contribution to popular thread
      "Training wheels without the bike", see merlyn's Re: Not Inciting a Holy War.

      Jan Stoklasa, Mistaker
      "For those about to Perl, we salute you"