perl 中的对象 就是引用 通过new方法传递数据结构给各个模块[root@wx03 test]# cat x1.pm package x1; use Data::Dumper; sub new { my $self ={};my $invocant = shift; my $class = ref($invocant) || $invocant; my ($name,$age,$starting_position,$monthly_salary)=@_; my $self = { "name" =>$name, "age" =>$age }; print "\$class is $class\n"; print "--------------------\n";print $self->{name};print "\n";print "--------------------\n";bless($self, $class); # 给予对象性质 print "\$self is $self\n";$str=Dumper($self);print "\$str is $str\n";return $self; }; sub sum_var { my ($self, $var1, # Name or IP number of host to ping $var2, # Seconds after which ping times out ) = @_;my $var3= $var1 + $var2;return $var3; } 1;[root@wx03 test]# cat a2.pl unshift(@INC,"/root/test"); require x1;$ed=x1->new('lily','29');print "\n";[root@wx03 test]# perl a2.pl $class is x1--------------------lily--------------------$self is x1=HASH(0xd49310) ###perl里对象就是hash$str is $VAR1 = bless( { 'name' => 'lily', 'age' => '29' }, 'x1' ); -----------------------------------------------------[root@wx03 test]# cat x1.pm package x1; use Data::Dumper; sub new { my $self ={};my $invocant = shift; my $class = ref($invocant) || $invocant; my ($name,$age,$starting_position,$monthly_salary)=@_; my $self = { "name" =>$name, "age" =>$age }; bless($self, $class); # 给予对象性质 return $self; }; sub sum_var { my ($self, $var1, # Name or IP number of host to ping $var2, # Seconds after which ping times out ) = @_;my $var3= $var1 + $var2;return $var3; } 1;[root@wx03 test]# cat x2.pm package x2; use Data::Dumper; sub sum_a { my ($self, ##传入对象 $var1, # Name or IP number of host to ping $var2, # Seconds after which ping times out ) = @_;print "x2 module\n";print $self->{name};print "\n";my $var3= $var1 + $var2 +99;return $var3; } 1;[root@wx03 test]# cat a1.pl unshift(@INC,"/root/test"); require x1;require x2;$ed=x1->new('lily','29');print "----------------------\n";print x2::sum_a($ed,90,66); ##传入对象到x2模块print "\n";[root@wx03 test]# perl a1.pl ----------------------x2 modulelily255