我正在尝试为 Web 索引程序解析 HTML 文档。为此,我使用HTML::TokeParser。
我的第一个 if 语句的最后一行出现错误:
if ( $token->[1] eq 'a' ) {
#href attribute of tag A
my $suffix = $token->[2]{href};
说的是Can't use string ("<./a>") as a HASH ref while "strict refs" in use at ./indexer.pl line 270, <PAGE_DIR> line 1.
我的问题是(后缀?或<./a>
?)是一个字符串,需要变成一个哈希引用吗?我查看了其他有类似错误的帖子..但我仍然对此不确定。谢谢你的帮助。
sub parse_document {
#passed from input
my $html_filename = $_[0];
#base url for links
my $base_url = $_[1];
#created to hold tokens
my @tokens = ();
#created for doc links
my @links = ();
#creates parser
my $p = HTML::TokeParser->new($html_filename);
#loops through doc tags
while (my $token = $p->get_token()) {
#code for retrieving links
if ( $token->[1] eq 'a' ) {
# href attribute of tag A
my $suffix = $token->[2]{href};
#if href exists & isn't an email link
if ( defined($suffix) && !($suffix =~ "^mailto:") ) {
#make the url absolute
my $new_url = make_absolute_url $base_url, $suffix;
#make sure it's of the http:// scheme
if ($new_url =~ "^http://"){
#normalize the url
my $new_normalized_url = normalize_url $new_url;
#add it to links array
push(@links, $new_normalized_url);
}
}
}
#code for text words
if ($token->[0] eq 'T') {
my $text = $token->[1];
#add words to end of array
#(split by non-letter chars)
my @words = split(/\P{L}+/, $text);
}
}
return (\@tokens, \@links);
}