gpt4 book ai didi

perl - 为什么直接设置 @ISA 在此示例中不起作用?

转载 作者:行者123 更新时间:2023-12-02 09:00:49 26 4
gpt4 key购买 nike

我有这两个模块:

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,则脚本可以正常工作。我试图重写一些方法并在之后调用原始方法。我做错了什么?

最佳答案

因为 G2.pm 需要包含 use G1; 行。否则,G1.pm 永远不会被加载。如果您运行时出现警告,Perl 会告诉您:

$ perl -w t.pl
Can't locate package G1 for @G2::ISA at t.pl line 1.
Can't locate package G1 for @G2::SUPER::ISA at G2.pm line 8.
Can't locate package G1 for @G2::SUPER::ISA at G2.pm line 8.
Can't locate object method "new" via package "G2" at G2.pm line 8.

请注意所有无法定位包 G1... 错误。

需要明确的是,use base 'G1' 可以工作,因为它也可以use G1

关于perl - 为什么直接设置 @ISA 在此示例中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1385422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com