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

I have a config file of sorts that suggests an action to be taken (like setVariable x or catVariable x newdata) and I am trying to write a script around it. The conf file allows the user to specify the names of variables so the script will not know them before hand.

The problem is whether or not it's possible to create variables from the output or value of other variables?

open(CONF," conf.file"); while(<CONF>) { ($variablecmd,$variablename,$newdata) = split(/ /, $_); if ($variablecmd eq "setVariable") { #create new variable named the current value of $variablename } elsif ($variablecmd eq "catVariable") { #assign data to varaible named $variablename } } close(CONF);


Is this possible?

Replies are listed 'Best First'.
Re: Creating new variables from the value of other variables
by larsen (Parson) on Jun 21, 2001 at 01:18 UTC
(jeffa) Re: Creating new variables from the value of other variables
by jeffa (Bishop) on Jun 21, 2001 at 01:22 UTC
    What you are referring to is known as soft links, and they are really not worth it. Instead use a hash. Also, i don't see why you need to differentiate between creating a variable and setting a variable. Perl knows what do to, so let's get rid of $variablecmd - you really probably don't need it.
    use strict; #dammit! my %hash; open(CONF," conf.file") || die 'didn't open for reading'; while(<CONF>) { my ($name,$data) = split; # look ma, no args! $hash{$name} = $data; } close CONF;
    Now you have all of your 'variables' and their values stored in one convient location a hash. Try this just after the close:
    use Data::Dumper; print Dumper %hash;
    Later in your code, when you need to access one of the config variables, say 'foo', you do so like this: $hash{foo}

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: Creating new variables from the value of other variables
by John M. Dlugosz (Monsignor) on Jun 21, 2001 at 01:22 UTC
    1) use a %hash to hold the values of interest.
    2) use symbolic references.
    3) use eval.

    In that order of preference.

    —John

Re: Creating new variables from the value of other variables
by malloc (Pilgrim) on Jun 21, 2001 at 01:23 UTC
    THTOWTDI Dammit! $$variablename = $newdata; will do what you want. I think that you should know this, even though i am probably going to get downvoted. I have read Dominus's excellent paper quoted above, and heartily agree with it. You should to. Consider using a hash, for instance with the key as "variable name" and value as value.

    hope this helps,
    -malloc
Re: Creating new variables from the value of other variables
by thraxil (Prior) on Jun 21, 2001 at 01:25 UTC

    i'm not really sure what the difference is between "setVariable" and "catVariable" in your code, but how about using a hashtable?

    my %vars; open(CONF," conf.file"); while(<CONF>) { ($variablecmd,$variablename,$newdata) = split(/ /, $_); if ($variablecmd eq "setVariable") { $vars{$variablename} = $newdata; } elsif ($variablecmd eq "catVariable") { #assign data to varaible named $variablename if(exists $vars{$variablename}) { $vars{$variablename} = $newdata; } else { die "assignment to undeclared variable"; } } } close(CONF);
    anders pearson // digital samurai
    personal      // http://www.columbia.edu/~anders/
    weblog       // http://thraxil.dhs.org/
    
Re: Creating new variables from the value of other variables
by thatguy (Parson) on Jun 21, 2001 at 01:29 UTC
    Now that I know it's possible (thanks malloc) I guess I'll try to do it right w/hashes.

    thanks all