Of course, you want to go the other way, if you merely want to convert a set of variables into a hash, there is no need to use globs or symbolic references. Just use eval, but this too can get you into trouble if you over use it. And be sure to put the variable sigal '$' into single quotes, e.g.
my %hash; $hash{alpha}{state} = eval('$A::alpha_state'); print '$hash{alpha}{state}=', $hash{alpha}{state}, "\n";
Or more generally,
our $alpha_state = 'STATE'; our $beta_state = 'state'; our $alpha_country = 'COUNTRY'; our $beta_country = 'country'; my %hash; # convert yucky legacy variables into nice hashes for my $k1 qw(alpha beta) { for my $k2 qw(country state) { $hash{$k1}{$k2} = eval '$'.$k1.'_'.$k2; } } # print out the hash for my $k1 (keys %hash) { for my $k2 (keys %{$hash{$k1}}) { print "$k1,$k2=",$hash{$k1}{$k2},"\n"; } } #outputs alpha,country=COUNTRY alpha,state=STATE beta,country=country beta,state=state
Update: reworded intro to take into account alternate reading of OP's question (see replies below).
In reply to Re: Mandatory Symbolic References?
by ELISHEVA
in thread Mandatory Symbolic References?
by bfreemer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |