为什么以下代码产生错误“无法调用方法”获取“在第6行的未定义值”?我已经通过类似的帖子阅读并认为我理解为什么会出现这种错误信息,但我不知道它在这里是如何应用的。我从WWW :: Mechanize模块的教程中复制了这段代码,所以我希望它可以解决问题。
#!/usr/bin/perl
use warnings ;
use strict ;
use WWW::Mechanize;
my $url = 'http://www.google.com';
my $m->get($url);
my $link = $m->find_link(text =>'Advanced Search');
print "The Google advanced search URL is: $link->url()\n";
答案 0 :(得分:5)
您错过了将WWW::Mechanize
类的实例分配给$m
变量的步骤:
use WWW::Mechanize;
my $m = WWW::Mechanize->new; # <-- HERE
my $url = 'http://www.google.com';
$m->get($url); # ...and remove 'my' from this line
my $link = $m->find_link(text =>'Advanced Search');
# also, a method call won't interpolate properly, so change
# the below line as well:
print "The Google advanced search URL is: " . $link->url() . "\n";
# or:
# my $result = $link->url();
# print "The Google advanced search URL is: $result\n";