<pre name="code" class="python"><pre name="code" class="python">uat-app01:/root/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); # 给予对象性质
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;
uat-app01:/root/test# cat x1.pl
unshift(@INC,"/root/test");
require x1;
$ed=x1->new('lily','29')
;
print $ed->sum_var(99,5);
print "\n";
print $ed->{name};
print "\n";
uat-app01:/root/test# perl x1.pl
$class is x1
--------------------
lily
--------------------
104
lily
当Perl 看到$ed->sum_var(99,5);
时,它会决定$ed属于哪个类(也就是在其中执行bless的)
在这里,它是x1.
Perl于是就会如下所示调用这个函数
x1::sum_var($ed) 换句话说,箭头左边的对象只是作为相应子程序的第一个参数。
uat-app01:/root/test# cat x1.pl
unshift(@INC,"/root/test");
require x1;
$ed=x1->new('lily','29');
print x1::sum_var($ed,90,66);
print "\n";
uat-app01:/root/test# perl x1.pl
$class is x1
--------------------
lily
--------------------
156