首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取Perl上的Bareword错误教程

获取Perl上的Bareword错误教程
EN

Stack Overflow用户
提问于 2010-06-26 07:36:30
回答 2查看 1.8K关注 0票数 2

我正在取得进步,但我遇到了一个新问题。

这是新的代码:

代码语言:javascript
复制
#!/usr/bin/perl -w
use strict;
use LWP::Simple;
use HTML::TreeBuilder;

my $url = 'http://oreilly.com/store/complete.html';
my $page = get( $url ) or die $!;
my $p = HTML::TreeBuilder->new_from_content( $page );
my($book);
my($edition);

my @links = $p->look_down(
    _tag => 'a',
    href => qr{^ /Qhttp://www.oreilly.com/catalog/\E \w+ $}x
);

my @rows = map { $_->parent->parent } @links;

my @books;
for my $row (@rows) {
    my %book;
    my @cells = $row->look_down( _tag => 'td' );
    $book{title}    =$cells[0]->as_trimmed-text;
    $book{price}    =$cells[2]->as_trimmed-text;
    $book{price} =~ s/^\$//;

    $book{url}      = get_url( $cells[0] );
    $book{ebook}    = get_url( $cells[3] );
    $book{safari}   = get_url( $cells[4] );
    $book{examples} = get_url( $cells[5] );
    push @books, \%book;
}

sub get_url {
    my $node = shift;
    my @hrefs = $node->look_down( _tag => 'a');
    return unless @hrefs;
    my $url = $hrefs[0]->atr('href');
    $url =~ s/\s+$//;
    return $url;
}

$p = $p->delete; #we don't need this anymore.

{
    my $count = 1;
    my @perlbooks = sort { $a->{price} <=> $b->{price} }
                    grep { $_->{title} =~/perl/i } @books;
    print $count++, "\t", $_->{price}, "\t", $_->{title} for @perlbooks;
}

{
    my @perlbooks = grep { $_->{title} =~ /perl/i } @books;
    my @javabooks = grep { $_->{title} =~ /java/i } @books;
    my $diff =  @javabooks - @perlbooks;
    print "There are ".@perlbooks." Perl books and ".@javabooks. " Java books. $diff more Java than Perl.";
}

for my $book ( $books[34] ) {
    my $url = $book->{url};
    my $page = get( $url );
    my $tree = HTML::TreeBuilder->new_from_content( $page );
    my ($pubinfo) = $tree->look_down(
                                    _tag => 'span',
                                    class => 'secondary2'
    );
    my $html = $pubinfo->as_HTML; print $html;
    my ($pages) = $html =~ /(\d+) pages/,
    my ($edition) = $html =~ /(\d)(?:st|nd|rd|th) Edition/;
    my ($date) = $html =~ /(\w+ (19|20)\d\d)/;

    print "\n$pages $edition $date\n";

    my ($img_node) = $tree->look_down(
                                    _tag => 'img',
                                    src  => qr{^/catalog/covers/},
    );
    my $img_url = 'http://www.oreilly.com'.$img_node->attr('src');
    my $cover = get( $img_url );
    # now save $cover to disk
}

现在我得到了这些错误,

在./SpiderTutorial_19_06.pl第23行使用"strict subs“时,不允许使用Bareword "text”。在./SpiderTutorial_19_06.pl第24行使用"strict subs“时,不允许使用Bareword "text”。由于编译错误,./SpiderTutorial_19_06.pl的执行中止。

任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

发布于 2010-06-26 07:40:17

我不知道最初的程序,但最有可能的as_trimmed-text应该是as_trimmed_text

票数 4
EN

Stack Overflow用户

发布于 2010-06-26 07:40:33

问题出在方法名as_trimmed-text上。在perl中,名称中不允许使用连字符。你可能指的是as_trimmed_text。现在它被解析为$cells[0]->as_trimmed() - text()

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3122086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档