不能使用字符串作为哈希引用..?

mde*_*ges 3 perl indexer

我正在尝试为Web索引程序解析HTML文档.为此,我使用HTML :: TokeParser.

我在第一个if语句的最后一行收到错误:

 if ( $token->[1] eq 'a' ) {
     #href attribute of tag A
     my $suffix = $token->[2]{href};
Run Code Online (Sandbox Code Playgroud)

说的是 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);
}
Run Code Online (Sandbox Code Playgroud)

Emi*_*Sit 8

get_token()方法返回一个数组,其中$token->[2]是一个哈希引用,其中包含href唯一的if $token->[0]是S(即开始标记).在这种情况下,您匹配结束标记(其中$token->[0]是E).有关详细信息,请参阅PerlDoc.

要修复,请添加

next if $token->[0] ne 'S';
Run Code Online (Sandbox Code Playgroud)

在循环的顶部.