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

lets say i have:
$childHood = "a young kid'; $test = "child";
i want to do something like:
$${test}Hood = "an old man";
AND IT RESETS $childHood eq "an old man";
HOW DO I DO THIS????????

Edit by castaway - added code tags

Replies are listed 'Best First'.
Re: 2 $vars 2 1 $var
by ysth (Canon) on Feb 10, 2005 at 22:44 UTC
    $childHood = "a young kid"; $test = "child"; { no strict "refs"; ${$test."Hood"} = "an old man"; }
    This is called a symbolic reference and is disallowed under use strict, hence the temporary disabling of strict refs.

    It will only work if $childHood is not a lexical (my) variable. If it is a lexical, your only recourse is eval.

    Note that needing to do this usually indicates you are not storing your data in the best possible way. Perhaps you could do something like:

    my %Hood; $Hood{child} = "a young kid"; $test = "child"; $Hood{$test} = "an old man";
    (last bit added simultaneously with Roy Johnson's comment below)
      Note that in most situations where you want to do something like this, it's often better to use a hash:
      my %hood = (child => 'a young kid'); $test = 'child'; $hood{$test} = 'an old man';

      Caution: Contents may have been coded under pressure.
Re: 2 $vars 2 1 $var
by moot (Chaplain) on Feb 10, 2005 at 22:37 UTC
A reply falls below the community's threshold of quality. You may see it by logging in.