Why doesn't the @ISA setting directly work in this example?
I have these two modules:
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;
and the following script:
use G2;
my $ob = G2->new();
$ob->three();
When I run the script, it throws the following error:
Can't locate object method "new" via package "G2" at G2.pm line 8.
If I replace the line @ISA
with the use base
script works. I am trying to override some methods and call them original. What am I doing wrong?
source to share
As G2.pm must include the line use G1;
. It G1.pm
never boots without it . If you run with warnings, Perl will tell you the following:
$ 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.
Note that it cannot find package errors G1 ...
And to be clear, it use base 'G1'
works because it also does use G1
.
source to share