I'm not sure you want to do what you are asking for. You could do
Update: Arien++ for bringing the error in the line below to my attention. I'd like to claim it was deliberate to prevent the unwary from doing this. It wasn't. It's just something I never do. I'm leaving it as-is with the flag.
my @array = qw( foo bar baz ); for (@array) { eval "$_ = 'value'"; # Error here!!! }
You now have three vars $foo='value'; $bar='value'; $baz='value';
but unless you know what these names are when you write your script--in which case why bother doing this at all--you have no convenient way of using these vars?
I think what your trying to acheive is an associative array or in Perl's terms, a hash. In which case, to set up a hash from the values of an array, use (for example)
#name your hash my %hash; #autovivify elements with names from the array.. #using undef simply means they will all be undef. # (all but the first one would be anyway, this just keeps things consi +stant). @hash{@array} = undef; # then to use your named array elements # eg. keyboard or web page. Assume we get the string 'foo' my $varName = getVarNameFromSomeWhere(); # then to use it $hash{$name} = 'we just gave $hash{foo} this as its value'; #and to print it print $hash{$varName}, "\n";
I hope I read your question correctly, and this is useful, if not, post a reply explaining a little (lot) more of what it is you are trying to achieve.
In reply to Re: dynamically creating variables named after the contents of an array
by BrowserUk
in thread dynamically creating variables named after the contents of an array
by gnu@perl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |