setting a variable from a string

A

Ashish

hi,

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 ?

thanks,
ashish
 
J

Josef Moellers

Ashish said:
hi,

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 ?

You can, but it's generally not advised to do so.
Rather use a hash and
$config{$x} = $y;
 
A

Average_Joe

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
 
P

Paul Lalli

Ashish said:
I want to set a variable.
The variables name is stored as a string in another variable.
^^^^^^^^^^^^^^^

Please check the FAQ that comes standard with Perl *before* posting:

perldoc -q "variable name"

That will tell you both *how* to do what you asked, and also *why not*
to do what you asked, and what you *should* be doing instead.

Paul Lalli
 
J

Jürgen Exner

Ashish said:
I want to set a variable.
The variables name is stored as a string in another variable. [...]
can I somehow set $LogDir using $x ?

Technically yes. Practically it is A VERY BAD IDEA (TM).
Please see the gazillions of previous postings about this topic (google for
"symbolic references" or "sym refs") and the FAQ "How can I use a variable
as a variable name?" (perldoc -q "variable name") about why it is a bad idea
and which much better solution to use instead.

jue
 
J

Jürgen Exner

Average_Joe said:
On 2005-08-12, Ashish <[email protected]> wrote:
[Asking for symbolic references]
Here's one way, dereference with: ${$x} = $y;

If you are showing bad practices then you at least you should also explain
that this conflicts with recommend coding practices, e.g. that it doesn't
work with lexical variables ("my") or under "use strict".
Just a pointer to the FAQ would have been enlightning.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top