继承例子:
zjzc01:/root/big2# cat Critter.pm
package Critter;
sub spawn {
my $self = {print "122334456"}; # 指向一个空的匿名散列
bless $self, "Critter"; # 把那个散列作成一个 Critter 对象
return $self; # 返回新生成的 Critter
};
1;
zjzc01:/root/big2# cat Horse.pm
package Horse;
#our @ISA=qw/Critter/;
use base (Critter);
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
color => "bay",
legs => 4,
owner => undef,
@_, # 覆盖以前的属性
};
return bless $self, $class;
};
1;
zjzc01:/root/big2# cat 3.pl
unshift(@INC,"/root/big2");
require Horse;
use Data::Dumper;
$x2=Horse->spawn;
my $xx= Dumper($x2);
print $xx;
print "
";
zjzc01:/root/big2# perl 3.pl
122334456$VAR1 = bless( {
'1' => undef
}, 'Critter' );
use base (Critter);
和
BEGIN{
require Critter;
@ISA=qw/Critter/;
}
这两种写法是等价