setting a variable from a string

Discussion in 'Perl Misc' started by Ashish, Aug 12, 2005.

  1. Ashish

    Ashish Guest

    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
     
    Ashish, Aug 12, 2005
    #1
    1. Advertising

  2. 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
     
    Josef Moellers, Aug 12, 2005
    #2
    1. Advertising

  3. Ashish

    Average_Joe Guest

    On 2005-08-12, Ashish <> 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
     
    Average_Joe, Aug 12, 2005
    #3
  4. Ashish

    Paul Lalli Guest

    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
     
    Paul Lalli, Aug 12, 2005
    #4
  5. 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
     
    Jürgen Exner, Aug 12, 2005
    #5
  6. Average_Joe wrote:
    > On 2005-08-12, Ashish <> 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
     
    Jürgen Exner, Aug 12, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    688
  2. mfglinux
    Replies:
    11
    Views:
    737
    Roberto Bonvallet
    Sep 12, 2007
  3. Mir Nazim
    Replies:
    2
    Views:
    358
    Mir Nazim
    Dec 21, 2008
  4. Johan Holmberg

    Setting "variable" global variable ?

    Johan Holmberg, Sep 24, 2003, in forum: Ruby
    Replies:
    6
    Views:
    148
    Alan Chen
    Sep 25, 2003
  5. David Filmer
    Replies:
    19
    Views:
    267
    Kevin Collins
    May 21, 2004
Loading...

Share This Page