2014年6月24日 星期二

perl 物件名稱和檔名

(備份自wordpress blog)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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

1
2
3
4
5
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

沒有留言:

張貼留言