文件:MyPerson.pm
package MyPerson;
use strict;
#use warnings;
use vars qw(@ISA); # 这一句用于 inheritance,也可以省略
sub new {
my ($class, @args) = @_;
my ($first,$last,$id,$father,$mother) = @args;
my $self = {
'fname' => $first,
'lname' => $last,
'id' => $id,
'mother' => $mother,
'father' => $father,
};
bless $self, $class;
return $self;
}
sub father {
my ($self,$val) = @_;
return $self->{'father'};
}
sub getId { return shift->{'id'} }
sub add {
my ($class, $a, $b) = @_;
return ($a + $b);
}
1;
调用文件:t.pl
#!/usr/bin/perl -w
use strict;
use Data::Dumper;
use MyPerson;
my $person = MyPerson->new('jimbo', 'gumbo', 1,2,3);
my @family = (undef,$person); # 0 index needs to be empty
push @family, MyPerson->new('mom', 'gumbo', 3, 0, 0);
push @family, MyPerson->new('dad', 'gumbo', 2, 0, 0);
my $dad = $family[$person->father];
print "father id is ", $person->father, "\n";
print "dad = ".Dumper($dad)."\n";
print "father obj id is $dad id is ", $dad->getId,"\n";
print "father obj id is $dad id is ", $dad->add(5, 2),"\n";
阅读(2292) | 评论(0) | 转发(0) |