我有這兩個模塊:爲什麼在這個例子中直接設置@ISA不起作用?
package G1;
sub new {
my $class = shift;
my $self = {
one => 1,
two => 2,
three => 3
};
bless $self,$class;
return $self;
}
sub three {
my $self = shift;
print "G1 green is ",$self->{three};
}
1;
package G2;
our @ISA = qw(G1);
#use base qw(G1);
sub new {
my $class = shift;
my $self = $class->SUPER::new();
$self->{three} = 90;
bless $self,$class;
return $self;
}
sub three {
my $self = shift;
print "G2 rox!\n";
$self->SUPER::three();
}
1;
和下面的腳本:
use G2;
my $ob = G2->new();
$ob->three();
當我運行該腳本,它產生以下錯誤:
Can't locate object method "new" via package "G2" at G2.pm line 8.
如果我更換@ISA
與use base
一致,該腳本起作用。我試圖覆蓋一些方法並在之後調用原始方法。我究竟做錯了什麼?
如果你得到一個錯誤,*任何東西*,如果你有第一次檢查「使用嚴格的;使用警告;」然後再試一次:)機會是你會更好地瞭解哪裏出了問題。 – Ether 2009-09-06 16:54:28