为什么我无法在以下代码中使用子对象调用父代的testmethod?Perl OO问题,继承 - 调用父方法
use strict;
use Data::Dumper;
my $a = C::Main->new('Email');
$a->testmethod();
package C::Main;
sub new {
my $class = shift;
my $type = shift;
$class .= "::" . $type;
my $fmgr = bless {}, $class;
$fmgr->init(@_);
return $fmgr;
}
sub init {
my $fmgr = shift;
$fmgr;
}
sub testmethod {
print "SSS";
}
package C::Main::Email;
use Net::FTP;
@C::Main::Email::ISA = qw(C::Main);
sub init {
my $fmgr = shift;
my $ftp = $fmgr->{ftp} = Net::FTP->new($_[0]);
$fmgr;
}
package C::Main::FTP;
use strict;
use Net::FTP;
@C::Main::Email::FTP = qw(C::Main);
sub init {
my $fmgr = shift;
$fmgr;
}
你不需要在每个包中重复'use strict;'。因为'strict'是一个词汇范围的编译指示,所以直到当前范围结束时才有效。包声明不会创建一个范围,所以如果'strict strict;'被放置在文件的顶部,它就在整个文件的范围内。 – 2011-03-02 21:40:06