2014年6月24日 星期二

perl 物件名稱和檔名

(備份自wordpress blog)


our @ISA = qw(Person2);    # inherits from Person 是package的名字

use Person;  //是package的檔名,在同一資料夾下。

===================================

# class Employee.pm
package Employee;
use Person;
use strict;
our @ISA = qw(Person2);    # inherits from Person

#constructor
sub new {
my ($class) = @_;

#call the constructor of the parent class, Person.
my $self = $class->SUPER::new();
$self->{_id}   = undef;
$self->{_title} = undef;
bless $self, $class;
print "Employee constructor\n";
return $self;
}

#accessor method for  id
sub id {
my ( $self, $id ) = @_;
$self->{_id} = $id if defined($id);
return ( $self->{_id} );
}

#accessor method for  title
sub title {
my ( $self, $title ) = @_;
$self->{_title} = $title if defined($title);
return ( $self->{_title} );
}

sub print {
my ($self) = @_;

# we will call the print method of the parent class
$self->SUPER::print;
$self->address->print;
}

1;
=========================================

#class Person.pm
package Person2;
use strict;
use Address;    #Person class will contain an Address

#constructor
sub new {
my ($class) = @_;
my $self = {
_firstName => undef,
_lastName  => undef,
_ssn       => undef,
_address   => undef
};
bless $self, $class;
print "Person2 constructor\n";
return $self;
}

#accessor method for Person first name
sub firstName {
my ( $self, $firstName ) = @_;
$self->{_firstName} = $firstName if defined($firstName);
return $self->{_firstName};
}

#accessor method for Person last name
sub lastName {
my ( $self, $lastName ) = @_;
$self->{_lastName} = $lastName if defined($lastName);
return $self->{_lastName};
}

#accessor method for Person address
sub address {
my ( $self, $address ) = @_;
$self->{_address} = $address if defined($address);
return $self->{_address};
}

#accessor method for Person social security number
sub ssn {
my ( $self, $ssn ) = @_;
$self->{_ssn} = $ssn if defined($ssn);
return $self->{_ssn};
}

sub print {
my ($self) = @_;

#print Person info
printf( "Name:%s %s\n\n", $self->firstName, $self->lastName );
}

1;
How do I use a Perl module from a relative location?
http://stackoverflow.com/questions/787899/how-do-i-use-a-perl-module-from-a-relative-location

use FindBin;
use lib "$FindBin::Bin/../lib"; // . /var/www/cgi-bin/project/guestbook/controller

print "\@INC is @INC\n";
// @INC is ../model /usr/local/lib/perl5 /usr/local/share/perl5 /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5 /usr/share/perl5

沒有留言:

張貼留言