我的perl代码如下所示:
my $ns = scraper {
process "table.table tr>td>a", 'files[]' => 'TEXT';
};
my $mres = $ns->scrape(URI->new($purlToScrape));
if ( ($#{$mres->{files}}) > 0 ) {
print Dumper($mres->{files} );
foreach my $key (keys %{ $mres->{files} }) {
print @{$mres->{files}}[$key]."\n";
}
}
跑步时:
$VAR1 = [
'Metropolis (1927)',
'The Adventures of Robin Hood (1938)',
'King Kong (1933)',
'The Treasure of the Sierra Madre (1948)',
'Up (2009)',
'Lawrence of Arabia (1962)',
];
Not a HASH reference at /root/bash-advanced-scripts/rotten.pl line 28.
第28行是这样的:
print @{$mres->{files}}[$key]."\n";
我该如何解决这个问题?
答案 0 :(得分:5)
错误来自上一行:
foreach my $key (keys %{ $mres->{files} })
$mres->{files}
是一个数组引用,因此您不能将其取消引用为哈希值。
只需使用
for my $file (@{ $mres->{files} }) {
print $file, "\n";
}