今天看见某断代码:
A.pm
CODE:
Package A;
...
sub test{
print "test\n";
}
B.pm...
sub test{
print "test\n";
}
CODE:
Package B;
use A;
B->test();
觉得比较奇怪,B并未显式从A继承,何以能使用A的test函数use A;
B->test();
最后发现A.pm里面重写了import函数
CODE:
sub import{
my $class = $_[0];
my $caller = caller(0);
unless($caller->isa('A')){
no strict 'refs';
print "importing A\n";
push @{"$caller\::ISA"}, $class;
}
}
这里的caller是指调用栈,caller(0)为最顶层的PACKAGE名,由于B调用A的import,所以为'B'my $class = $_[0];
my $caller = caller(0);
unless($caller->isa('A')){
no strict 'refs';
print "importing A\n";
push @{"$caller\::ISA"}, $class;
}
}
B::ISA为父类数组
请发表评论