I want to set a variable.
The variables name is stored as a string in another variable.
I have
$x = "LogDir";
$y = "/tmp/";
I want to be able to set:
$LogDir = "/tmp/";
can I somehow set $LogDir using $x ?
Here's one way, dereference with: ${$x} = $y;
$var="LogDir";
$val="/tmp";
${$var} = $val;
print $LogDir,"\n";
Another way is something along the lines of evil^H^Hal.
Something like this for the risk taker:
eval("$var = \$val");
These are great fun, especially if '$var' is comming in from the outside, say,
a form variable, world writable file or other hostile input source.
It adds the "feature" that users can set arbitrary variables and run whatever
perl code they may want. It also makes perl code a real joy to read when trying
to figure out how a given variable is used.
I'd suggest finding another way, like maybe an associative array or even a tied
hash. The wonderful thing about perl is that there are all kinds of tricks
which can be used when the more sane ones don't work. Unfortunately, tricks are
a problem when it's time to figure out whats happening or how 'Joe hax0r'
managed to get in.
Eval in a curly braces context is perfectly OK, and even encouraged
where appropriate.
Jamie