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

i have an array something like
@array = ('var1=x','var2=y','var3=z');
i want to loop through creating $var1 = 'x'; $var2 = 'y' and $var3 = 'z'; as in
foreach (@array) { my ($var,$val) = split(/=/,$_); $$var = $val; }
But obviously that doesn't work or i wouldn't be asking. So what exactly is the correct syntax? Your help is much appreciated.

Replies are listed 'Best First'.
(Ovid) Re: A variable variable.
by Ovid (Cardinal) on Nov 23, 2000 at 04:16 UTC
    Go to this link and look for "why it's stupid to use a variable as a variable name".

    Cheers,
    Ovid

    Update: Yeah! What he said!

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

        Anyone who has not read this link should.

        Parts 2 and 3 are one of the best statements I have ever read for why good programming style really is good programming style.

Re: A variable variable.
by quidity (Pilgrim) on Nov 23, 2000 at 04:23 UTC

    Actually, that does work:

    bash-2.03$ perl -e'$foo = "bar"; $$foo = 1; print "$bar\n"' 1

    But like so many things which work, you don't want to do it like that. You are using something called symbolic references, which although allowed by perl are dangerous if you ever let a user get near @array. For instance, they could alter the value of any other variable in your program (like $path_to_utility).

    You can do something simillar using hashes:

    foreach (@array) { my ($key, $val) = split(/=/, $_); $vars{$key} = $val; }

    Or, if you are actually using var1, var2 etc in your program, you might just want to use an array:

    foreach (@array) { next unless /var(\d+)=(.*)/; $values[$1] = $2; }
      You are missing the point that it is a really, really bad idea to do this for reasons explained by everyone else. Use a hash instead:
      foreach my $var (@array) { my @val = split /=/, $var, 2; $hash{$val[0]} = $val[1]; }
      UPDATE
      I must have accidentally hit the wrong reply. This was meant as a reply to spaz above.
Re: A variable variable.
by rbaldwin (Novice) on Nov 23, 2000 at 18:41 UTC
    Wow thanks folks! The intent of my obviously controversial snippet of code was to save myself some work. I was taking an existing bit of transient cgi, and enhancing it so that I could save all my param()'s to a file as "var=val" pairs in a file on the server. When the user came back at some later date they could pick up where they left off.

    If i was doing this from scratch I would go with the hash but i was being lazy and didn't want to modify the original code too much.

    And thanks for the reference to a dissertation on perl stupidity, there must be many of us out there to require a 3 part article!

    And finally I do very much appreciate this forum and your quick responses, I left the question on my way out from work and upon my return in the morning 3 responses. I couldn't pay for better support.

Re: A variable variable.
by spaz (Pilgrim) on Nov 23, 2000 at 06:07 UTC
    Provided that all you're going to have is scalars, you could use something like this:
    #!/usr/bin/perl @array = ('var1=x','var2=y','var3=z'); foreach ( @array ) { s/^([^=]+?)=(.+?)$/\$$1 = "$2"/; eval; print "$_\n"; } print "$var1 $var2 $var3\n";
    Or did I miss something?