我目前使用“Moose”包从 Perl OOP 开始。
编译器抱怨它“无法在 Parser.pm 第 16 行修改非左值子例程调用”。
我不太明白为什么我不能只分配一个新对象。我猜有更好或更有效的方法来使用 Moose 执行可选参数?
#!/usr/bin/perl -w
package Parser;
use Moose;
require URLSpan;
require WWW::Mechanize;
has 'urlspan' => (is => 'rw', isa => 'URLSpan', required => 1);
has 'mech' => (is => 'rw', isa => 'WWW::Mechanize');
sub BUILD {
my $self = shift;
if(!$self->mech) {
warn("no Mech set for " . $self->urlspan->name);
$self->mech = WWW::Mechanize->new(agent => 'Mozilla/5.0 (Windows; U; Windows NT 6.0; de; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.4',
stack_depth => 1
); #line 16
}
}