我想动态创建一个%detail
哈希,而不使用eval
语句。此代码与eval
语句一起工作正常,但有没有更好的方法来执行此操作而不使用eval
?
my @input=('INFO: Vikram 32 2012','SAL: 12000$','ADDRESS: 54, junk, JUNK');
my %matching_hash= (
qr/^INFO:\s*(\S+)\s+(\S+)\s+(\S+)/ =>['name','age','joining'],
qr/^SAL:\s*(\S+)/ => ['salary'],
qr/ADDRESS:\s*(.*)/ =>['address']
);
my %detail;
while(my ($regex, $array) = each(%matching_hash)) {
foreach (@input){
if(/$regex/) {
for(my $i=0;$i<=$#$array; $i++) {
$j=$i+1;
eval '$detail{$array->[$i]} = $$j';
}
}
}
}
use Data::Dumper;
print Dumper(\%detail);
++++++++++++++
$VAR1 = {
'name' => 'Vikram',
'address' => '54, junk, JUNK',
'age' => '32',
'joining' => '2012',
'salary' => '12000$'
};