【发布时间】:2011-10-30 05:51:52
【问题描述】:
我收到了错误
Undefined subroutine &main::1 called at /usr/local/lib/perl/5.10.0/HTML/Parser.pm line 102.
这是我的代码
#open (IN, "<", "foo.html") or die "can't open source file: $!";
my $p = HTML::Parser->new( api_version => 3,
start_h => [&start, "tagname, attr, text"],
text_h => [&text, "text"],
default_h => [sub { print OUT shift }, "text"],
);
$p->utf8_mode;
$p->empty_element_tags;
$p->ignore_elements(qw(br));
$p->parse_file("foo.html") or die "parsing failed: $!";
#while (<IN>) {
# $p->parse($_) || die "parsing failed: $!";
#}
#$p->eof;
#close IN;
正如您在注释掉的部分中看到的那样,我也尝试过直接打开和调用 parse(运气同样不佳)。
文件确实可以正常打开。
Parser.pm line 102 是错误提到的是 parse_file 子例程,特别是调用 ->parse
的行我不知道 parse 在哪里,它不在 HTML::Parser 中,我也没有在 HTML::Entities 中找到它,这是 HTML::Parser 唯一的依赖项。 =/ 恐怕我现在迷路了,PERL 最深奥的魔法对我来说仍然是个谜。
【问题讨论】:
-
你在使用
use strict; use warnings;吗? -
fwiw,方法
parse显然是一个 XS 例程(即它是用 C 实现的)
标签: perl html-parsing