在tutorial中,有一個Person類的代碼。你能向我解釋第21/27行的目的嗎?我理解像$ _和@_這樣的概念,並且我知道my
用於聲明本地引用,但我不瞭解這些代碼上下文中的那些行。在Perl函數中聲明變量
1 #!/usr/bin/perl
2
3 package Person;
4
5 sub new
6 {
7 my $class = shift;
8 my $self = {
9 _firstName => shift,
10 _lastName => shift,
11 _ssn => shift,
12 };
13 # Print all the values just for clarification.
14 print "First Name is $self->{_firstName}\n";
15 print "Last Name is $self->{_lastName}\n";
16 print "SSN is $self->{_ssn}\n";
17 bless $self, $class;
18 return $self;
19 }
20 sub setFirstName {
21 my ($self, $firstName) = @_;
22 $self->{_firstName} = $firstName if defined($firstName);
23 return $self->{_firstName};
24 }
25
26 sub getFirstName {
27 my($self) = @_;
28 return $self->{_firstName};
29 }
30 1;
如果您有興趣做OO Perl中,我會強烈建議考慮看看[穆斯(https://metacpan.org/module/Moose),這使得很多是重複的樣板消失。有一個很好的介紹駝鹿在(免費)的書[現代的Perl](http://onyxneon.com/books/modern_perl/) – friedo
[perlootut](http://search.cpan.org/perldoc?perlootut)是官方文檔中的現代Perl OO教程。 – daxim