perl 改变对象属性

时间:2021-03-11 06:36:30
[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 fun2 {
$self=shift;
$i=shift;
$var=$self->{owner};
$self->{owner}="$var.$i";
};
sub on_receive_msg:lvalue{
$self=shift;
$self->{name}="aabbccdd123141242";
$self->{on_receive_msg};
}; our $str="12312";
1;
[root@wx03 test]# cat t10.pl
unshift(@INC,"/root/test");
use Horse;;
use Critter;
my $ua=Horse->new(); $ua->fun2($ARGV[0]);
print $ua->{owner};
print "\n"; [root@wx03 test]# perl t10.pl 1
scan.1
[root@wx03 test]# perl t10.pl 2
scan.2
[root@wx03 test]# perl t10.pl 3
scan.3