2011-08-19 29 views
3

我收到错误Perl HTML ::解析器错误;未定义子程序及主要:: 1

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; 

正如你可以在注释部分我还试图直接打开并调用解析看(同样运气不多)。

该文件打开正常。

Parser.pm线102这是错误中提到的是parse_file子程序,特别是排队叫号 - >解析

我不知道在那里分析是,它不是在HTML解析器::也没有我发现它在HTML :: Entities中,唯一的依赖HTML :: Parser具有。 = /恐怕我在这一点上迷失了,PERL最深的魔法对我来说仍然是个谜。

+0

你使用'use strict;使用警告;'? – TLP

+0

fwiw,method'parse'显然是一个XS例程(即它在C中实现) – ErikR

回答

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"], 
    ); 

否则你逝去的呼叫start()text(),而不是对它们的引用作为潜艇的结果。

+0

感谢您的快速响应,它的工作! –

+0

是的,它工作,因为它是正确的:) – vol7ron

3

在文档中说它应该使用\&start。如果排除反斜杠,它将使用函数start中的返回值(它将使用@_作为参数列表,如使用&的普通子例程调用编译指示)。该值可能是1

下面是一个例子:

C:\perl>perl -we "$c=\&s; sub s { print 'yada' }; $c->();" 
yada 
C:\perl>perl -we "$c=&s; sub s { print 'yada' }; $c->();" 
Undefined subroutine &main::1 called at -e line 1. 
yada 

不知道为什么错误就会来这里,但你可能会改变它,看它是否有帮助。

哦,还有,它看起来好像你没有使用use strict。当使用严格的,我得到一个更实用的错误:

C:\perl>perl -we "use strict; my $c=&s; sub s { print 'a' }; $c->();" 
Can't use string ("1") as a subroutine ref while "strict refs" in use at -e line 
+0

谢谢,引用sub工作!严格被使用= /但这整个事情已经很奇怪,我只是很高兴现在正在工作。 –

+0

@尼克约翰逊:严格的词汇范围;显然HTML/Parser.pm不使用它 – ysth

+0

@ysth奇怪的是,HTML/Parser.pm使用严格。 – TLP