Reputation: 4807
In this tutorial, there is code for a Person class. Are you able to explain to me the purpose of line 21/27? I understand concepts like $_ and @_, and I know my
is used for declaring local references, but I don't understand those lines in this code context.
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;
Upvotes: 0
Views: 324
Reputation: 6798
Since you understand the concept of @_
you would realise that you could just access these using shift
or $_[0]
as you need them. The reason for having these lines is more of a best practice. Since Perl doesn't have formal parameters, we normally assign them to named variables at the beginning of the subroutine before doing anything else. In this way, the code clearly shows what the subroutine expects and what each parameter is. (A reason why you might choose not to do this is for code efficiency, but normally you should choose code clarity first.)
Note the parentheses around the variables. This is to assign the @_
array to the list containing ($self, $firstname)
. If you don't have the parentheses, it won't work because it is trying to assign an array to multiple scalars. Note that if @_
contains more parameters, they will be ignored.
The list is declared using my
. This means that those scalars will exist only within this subroutine. This is for safety so that, if you use a scalar with the same name somewhere else, you won't get a conflict with unexpected results. Note that you need to have a line with use strict
at the top of your file for my
to be enforced.
$self
is used for object-oriented Perl. It will always the first parameter to a method. So in a call like $obj->firstname($foo)
, $obj
will be assigned to $self
inside your method, and $foo
will be assigned to $firstname
.
Upvotes: 1
Reputation: 262842
20 sub setFirstName {
21 my ( $self, $firstName ) = @_;
At the most basic level, this line takes the first two arguments to the subroutine and assigns them to the local variables $self
and $firstName
.
$person->setFirstName('jeeves');
In the context of object-oriented Perl, the first parameter passed to the method (because this is what the subroutine has become) is a reference to the instance on which the method is invoked ($person
is the above example). You need that reference to get to other methods and instance state. It is customary to call it $self
. In other languages, there would be something like this
built into the language, so that you do not have to extract it manually.
After that first special parameter are the other ("normal") arguments to the method.
Upvotes: 3
Reputation: 4454
This is a standard setter, it is setting the _firstName variable for the person object. Now going line by line
my ( $self, $firstName ) = @_;
This is assigning the first two arguments with which the setFirstName is called to $self and $firstName respectively, it is same as
$self = $_[0]; $firstName = $_[1];
Next
$self->{_firstName} = $firstName if defined($firstName);
This is assigning the $self person object's _firstName variable to $firstName which was defined in the previous line and doing this only if $firstName is defined
$self->{_firstName} = $firstName if defined($firstName);
Returns the new _firstName of the self object, it may not necessarily be changed
Upvotes: 1