Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl Can';t调用方法";标题“;在search.pl第10行的未定义值上_Perl_Mojolicious - Fatal编程技术网

Perl Can';t调用方法";标题“;在search.pl第10行的未定义值上

Perl Can';t调用方法";标题“;在search.pl第10行的未定义值上,perl,mojolicious,Perl,Mojolicious,获取错误:无法对search.pl第10行的未定义值调用方法“headers” 我该怎么办 非常感谢您忘记了这一点,您需要首先从tx获得res #!/usr/bin/perl use Mojo::Base -strict; use Mojo::DOM; use Mojo::Util qw(decode); use Mojo::UserAgent; my $uri = 'http://efremova.info/word/statja.html'; my $sel = 'td#centerCnt

获取错误:无法对search.pl第10行的未定义值调用方法“headers”

我该怎么办


非常感谢

您忘记了这一点,您需要首先从tx获得res

#!/usr/bin/perl
use Mojo::Base -strict;
use Mojo::DOM;
use Mojo::Util qw(decode);
use Mojo::UserAgent;
my $uri = 'http://efremova.info/word/statja.html';
my $sel = 'td#centerCnt ol li';
my $charset = 'windows-1251';
my $tx = Mojo::UserAgent->new()->get($uri);
my $res->headers->content_type("text/html; charset=$charset");
my $dom = $res->dom;
my $el = $dom->at($sel) or die "selector $sel not found";
$el->find('span.nobr')->each(sub { $_->replace($_->text) });
my $text = $el->text;
binmode(STDOUT, ':encoding(UTF-8)');

带有
my
的变量声明的结构如下:

my $tx = Mojo::UserAgent->new()->get($uri);

my $res = $tx->res;

$res->headers->content_type("text/html; charset=$charset");
my $dom = $res->dom;
(带括号的零件是可选的。)

当您刚刚执行
my$name
时,您声明了一个新变量,但尚未分配给它,因此该值为
undef
。请注意,在表达式中,正在定义的变量是不可访问的

表达式
undef eq(my$undef)
的计算结果为true:新变量的值为
undef
。 实际上,声明本身就是表达式,返回左值

my [TYPE] NAME [ATTRIBUTES] [= EXPRESSION]
在非严格模式下,您可以说
my$world=$world+2
,这将计算为
2
。在严格模式下,这是不允许的,除非您有一个同名的全局变量

您的语法
my$undefined->method\u call
有点不寻常,计算结果为
(unde)->method\u call
,这是不可能的(自动装箱除外)

解决方案:

  • 使用严格;使用警告

  • 首先声明并初始化变量,然后对其调用方法

  • 在这种特殊情况下,要检索内容类型,可以执行以下操作

    $ perl -Mstrict -E'my $three = my $foo + 2 + (my $bar=1); say $three'
    3
    
    my $content_type = $tx->res->headers->content_type;
    
    my $dom = $tx->res->dom;
    
    设置内容类型没有任何意义。要检索DOM,可以执行以下操作

    $ perl -Mstrict -E'my $three = my $foo + 2 + (my $bar=1); say $three'
    3
    
    my $content_type = $tx->res->headers->content_type;
    
    my $dom = $tx->res->dom;
    
    如果你喜欢长的方法链,你可以

    $ perl -Mstrict -E'my $three = my $foo + 2 + (my $bar=1); say $three'
    3
    
    my $content_type = $tx->res->headers->content_type;
    
    my $dom = $tx->res->dom;
    
    关于Mojo模块的文档:

    有关我的
    的文档:


    (建议使用以下链接)

    mojo
    重新标记为
    mojolicious
    ,因为这是您在此处使用的web框架的名称。