perl 类里的函数调用其他类的函数:
package Horse;
use base qw(Critter);
sub new {
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my $self = {
color => "bay",
legs => 4,
owner => undef,
@_, # 覆盖以前的属性
};
return bless $self, $class;
#return $self;
};
sub sum1 {
$self=shift;
my $a=shift;
my $b=shift;
return $a + $b + 7;
};
our @arr=qw/1 2 3 4 5 6 7/;
our %h1=(1,2,3,4,5,6,7,8);
1;
[root@wx03 test]# cat Critter.pm
package Critter;
unshift(@INC,"/root/test");
use message;
sub new {
my $self = {};
my $invocant = shift;
my $class = ref($invocant) || $invocant;
my ($name)=@_;
my $self = {
"name" =>$name
};
bless $self, $class; # Use class name to bless() reference
return $self;
};
sub sum2 {
$self=shift;
my $a=shift;
my $b=shift;
return $a + $b;
};
sub fun1 {
$self=shift;
message::message($self);
};
sub on_receive_msg:lvalue{
$self=shift;
$self->{name}="aabbccdd123141242";
$self->{on_receive_msg};
};
our $str="12312";
1;
[root@wx03 test]# cat message.pm
package message;
sub message {
my $self=shift;
$self->{color}="q3szczxcae2";
return "message 31313131\n";
};
1;
[root@wx03 test]# cat t10.pl
unshift(@INC,"/root/test");
use Horse;;
use Critter;
my $ua=Horse->new();
print $ua->fun1();
print $ua->{color};
print "\n";
[root@wx03 test]# perl t10.pl
message 31313131
q3szczxcae2