无法对未定义的值调用方法 X
Can't call method X on an undefined value
为什么以下代码会在第 6 行的未定义值上产生错误 "Can't call method "get"?我通读了类似的帖子,认为我明白为什么会出现这种错误消息,但我不明白它如何适用于此处。我从 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";
您错过了将 WWW::Mechanize
class 的实例分配给 $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";
为什么以下代码会在第 6 行的未定义值上产生错误 "Can't call method "get"?我通读了类似的帖子,认为我明白为什么会出现这种错误消息,但我不明白它如何适用于此处。我从 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";
您错过了将 WWW::Mechanize
class 的实例分配给 $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";