Perl Can';t调用方法";标题“;在search.pl第10行的未定义值上
获取错误:无法对search.pl第10行的未定义值调用方法“headers” 我该怎么办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
非常感谢您忘记了这一点,您需要首先从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框架的名称。