If the variables are declared already above your loop, then you can do it via string eval. E.g.
#!/usr/bin/perl
use strict;
use warnings;
my ($blah, $boo, $hoo); # DECLARED HERE
my %data = ( "blah"=>1, "boo"=>2, "hoo"=>3 );
# assign each key to a variable of the same name
foreach (keys %data) {
eval " \$$_ = \$data{$_} ";
} # end-foreach
print $blah;
exit;
Replace is still probably a better long run strategy.
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
|