在我的代码的第70行中,Perl 5.30.x中出现“ NOT HASH reference”错误,我在其中尝试测试$ xml的类型:
由于我离开Perl已有相当一段时间了,因此我不知道如何解决该问题,并且希望获得所有建议。
代码遵循(希望如此)。
#!/usr/bin/perl
use strict;
use URI;
use LWP::Simple;
use Net::Amazon;
use XML::Simple;
use constant AMAZON_TOKEN => 'amazon token deleted';
use constant DEBUG => 0;
# get our arguments. the first argument is the
# URL to fetch, and the second is the output.
my $url = shift || die "$0 <url> [<output>]\n";
my $output = shift || '/www/htdocs/cloud.html';
# we'll need to fetch the Alexa XML at some point, and
# we'll do it a few different times, so we create a
# subroutine for it. Using the URI module, we can
# correctly encode a URL with a query. In fact, you'll
# notice the majority of this function is involved with
# this, and at the end we use LWP::Simple to actually
# download and return the XML.
#####################################################
sub fetch_xml {
my $url = shift;
$url = "http://$url" unless $url =~ m[^http://];
warn "Fetching Alexa data for $url\n" if DEBUG;
my @args = (
cli => 10, dat => 'snba',
ver => '7.0', url => $url,
);
my $base = 'http://data.alexa.com/data';
my $uri = URI->new( $base );
$uri->query_form( @args );
$uri = $uri->as_string;
return get( $uri );
}
# raw XML is no good for us, though, as we want to extract
# particular items of interest. we use XML::Simple to turn
# the XML into Perl data structures, because it's easier
# than fiddling with event handling (as with XML::Parser
# or XML::SAX), and we know there's only a small amount of
# data. we want the list of related sites and the list of
# related products. we extract and return both.
#####################################################
sub handle_xml {
my $page = shift;
my $xml = XMLin( $page );
my @related = map {
{
asin => $_->{ASIN},
title => $_->{TITLE},
href => $xml->{RLS}{PREFIX}.$_->{HREF},
}
} @{ $xml->{RLS}{RL} };
my @products;
if (ref $xml->{SD}{AMZN}{PRODUCT} eq 'ARRAY') {
@products = map { $_->{ASIN} } @{ $xml->{SD}{AMZN}{PRODUCT} };
} else { @products = $xml->{SD}{AMZN}{PRODUCT}{ASIN}; }
return ( \@related, \@products );
}
# Functions done; now for the program:
warn "Start URL is $url\n" if DEBUG;
my @products; # running accumulation of product ASINs
{
my $page = fetch_xml( $url );
my ($related, $new_products) = handle_xml( $page );
@products = @$new_products; # running list
for (@$related) {
my $xml = fetch_xml( $_->{href} );
my ($related, $new_products) = handle_xml( $page );
push @products, @$new_products;
}
}
# We now have a list of products in @products, so
# we'd best do something with them. Let's look
# them up on Amazon and see what their titles are.
my $amazon = Net::Amazon->new( token => AMAZON_TOKEN );
my %products = map { $_ => undef } @products;
for my $asin ( sort keys %products ) {
warn "Searching for $asin...\n" if DEBUG;
my $response = $amazon->search( asin => $asin );
my @products = $response->properties;
die "ASIN is not unique!?" unless @products == 1;
my $product = $products[0];
$products{$asin} = {
name => $product->ProductName,
price => $product->OurPrice,
asin => $asin,
};
}
# Right. We now have name, price, and
# ASIN. Let's output an HTML report:
{
umask 022;
warn "Writing to $output\n" if DEBUG;
open my $fh, '>', $output or die $!;
print $fh "<html><head><title>Cloud around $url</title></head><body>";
if (keys %products) {
print $fh "<table>";
for my $asin (sort keys %products) {
my $data = $products{$asin};
printf $fh "<tr><td>".
"<a href=\"http://amazon.com/exec/obidos/ASIN/%s\">".
"%s</a></td> <td>%s</td></tr>",
@{$data}{qw( asin name price )};
}
print $fh "</table>";
}
else { print $fh "No related products found.\n"; }
print $fh "</body></html>\n";
}
答案 0 :(得分:1)
您正在使用XML::Simple。那是一个错误。甚至该模块的文档也与我一致:
此模块的状态
强烈建议在新代码中使用此模块 泄气。其他模块可提供更多 简单,一致的界面。特别是XML::LibXML 强烈建议使用,您可以参考Perl XML::LibXML by Example 以获得教程简介。
XML::Twig是另一个很好的选择。
此模块的主要问题是选项众多 (其中一些不幸的默认值)以及任意方式 这些选项相互影响-通常会产生意想不到的结果。
欢迎提供具有错误修复和文档修复的补丁程序,但它们是新的 功能不太可能添加。
没有看到您的XML很难确定,但这是我认为已经发生的事情。 XML :: Simple在返回的数据结构中众所周知不一致。输入文档中的少量更改会对返回的数据结构产生重大影响。在一个非常相似的文档上,什么是哈希引用变成数组引用是很常见的。
我的建议是停止使用XML :: Simple。就个人而言,我将其替换为XML :: LibXML。