【发布时间】:2014-07-17 07:29:53
【问题描述】:
我正在编写一个继承自 HTML::Parser 的 Perl 模块,但我收到了一个编译错误,我从昨天开始就无法解决。这是代码:
#!/usr/bin/perl
# HTML::Parser : The inheriting module
# Fetches all links and their text from a HTML file
use warnings;
use strict;
use HTML::Entities;
package AnchorTextParser;
use base 'HTML::Parser';
our $betweenAnchor;
our $linkAdress;
our %anchorTexts;
sub start {
my (undef, undef, $attr) = @_;
if( $attr->{href} && not ($attr->{href} =~ /mailto/i) ) {
$betweenAnchor = 1;
$linkAdress = $attr->{href};
}
}
sub end {
$betweenAnchor = 0;
}
sub text {
if( $betweenAnchor ) {
my $origText;
(undef, $origText) = @_;
#print decode_entities($origText) . "\n";
%anchorTexts{$linkAdress} = decode_entities($origText);
}
}
sub getAnchorTexts {
return\%anchorTexts;
}
1;
我加载模块时遇到的错误是:
syntax error at AnchorTextParser.pm line 34, near "%anchorTexts{"
Global symbol "$origText" requires explixit package name at AnchorTextParser.pm line 34.
syntax error at AnchorTextOarser.pm line 36, near "}"
Compilation failed in require at module_loader.pl line 6.
BEGIN failed--compilation aborted at module_loader.pl line 6.
为了避免混淆,这是模块加载器中发生的事情:
#!/usr/bin/perl
# HTML::Parser : The test script
use warnings;
use strict;
use AnchorTextParser;
这是我第一次在 Perl 中编写继承模块,所以这很可能是我做错了什么。但是错误是如此的难以描述,而且显然是错误的($origText 不是全局的,并且非常明确地定义为“我的”..),以至于我无法弄清楚错误是什么。 非常感谢任何帮助。
【问题讨论】:
标签: perl inheritance html-parsing