Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > setting a variable from a string

Reply
Thread Tools

setting a variable from a string

 
 
Ashish
Guest
Posts: n/a
 
      08-12-2005
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

 
Reply With Quote
 
 
 
 
Josef Moellers
Guest
Posts: n/a
 
      08-12-2005
Ashish wrote:
> 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;


--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
 
 
 
Average_Joe
Guest
Posts: n/a
 
      08-12-2005
On 2005-08-12, Ashish <(E-Mail Removed)> wrote:
> 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
--
http://www.geniegate.com Custom web programming
User Management Solutions Perl / PHP / Java / UNIX

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      08-12-2005
Ashish wrote:
> 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

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      08-12-2005
Ashish wrote:
> 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


 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      08-12-2005
Average_Joe wrote:
> On 2005-08-12, Ashish <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question: Evaluate an string variable's value to a variable Mir Nazim Python 2 12-21-2008 07:05 AM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Setting an environment variable from another environment variable marcwentink@hotmail.com Java 5 04-04-2007 10:39 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
Setting "variable" global variable ? Johan Holmberg Ruby 6 09-25-2003 03:42 PM



Advertisments