Perl HTML :: parser错误; 未定义的子程序&main :: 1

Nic*_*son 3 perl html-parsing

我收到了错误

Undefined subroutine &main::1 called at /usr/local/lib/perl/5.10.0/HTML/Parser.pm line 102.
Run Code Online (Sandbox Code Playgroud)

这是我的代码

#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;
Run Code Online (Sandbox Code Playgroud)

正如你在注释掉的部分中看到的那样,我也尝试直接打开并调用解析(同样运气不大).

该文件确实打开.

Parser.pm第102行是错误提及的是parse_file子例程,特别是调用行 - >解析

我不知道parse在哪里,它不在HTML :: Parser中,也没有在HTML :: Entities中找到HTML :: Parser唯一的依赖.= /我担心我在这一点上迷失了,PERL最深的魔法对我来说仍然是一个谜.

Eri*_*ikR 7

尝试使用\&start\&text:

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"],
    );
Run Code Online (Sandbox Code Playgroud)

否则你传递调用的结果,start()text()不是作为subs传递它们.