在 Perl 函数中声明变量
在此教程中,有一个 Person 类的代码。你能向我解释一下第21/27行的目的吗?我理解 $_ 和 @_ 等概念,并且我知道 my
用于声明本地引用,但我不理解此代码上下文中的这些行。
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在最基本的层面上,这一行将前两个参数传递给子例程,并将它们分配给局部变量
$self
和$firstName
。在面向对象的 Perl 上下文中,传递给方法的第一个参数(因为这就是子例程)是对调用该方法的实例的引用(
$person
是上面的例子)。您需要该引用来获取其他方法和实例状态。人们习惯上称其为$self
。在其他语言中,语言中会内置类似this
的内容,因此您不必手动提取它。在第一个特殊参数之后是该方法的其他(“正常”)参数。
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
.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 likethis
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.
这是一个标准设置器,它为 person 对象设置 _firstName 变量。现在逐行将调用
相同
setFirstName 的前两个参数分别分配给 $self 和 $firstName,与Next
这是将 $self person 对象的 _firstName 变量分配给前面定义的 $firstName行并且仅当定义了 $firstName 时才执行此操作
返回 self 对象的新 _firstName,不一定会更改
This is a standard setter, it is setting the _firstName variable for the person object. Now going line by line
This is assigning the first two arguments with which the setFirstName is called to $self and $firstName respectively, it is same as
Next
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
Returns the new _firstName of the self object, it may not necessarily be changed
既然您了解了
@_
的概念,您就会意识到您可以根据需要使用shift
或$_[0]
来访问它们。拥有这些行的原因更多的是最佳实践。由于 Perl 没有形式参数,因此我们通常在子例程开始时将它们分配给命名变量,然后再执行其他操作。这样,代码就清楚地显示了子程序所期望的内容以及每个参数是什么。 (您可能选择不这样做的原因是为了代码效率,但通常您应该首先选择代码清晰度。)请注意变量周围的括号。这是将
@_
数组分配给包含($self, $firstname)
的列表。如果没有括号,它将不起作用,因为它试图将一个数组分配给多个标量。请注意,如果@_
包含更多参数,它们将被忽略。该列表使用
my
声明。这意味着这些标量将仅存在于该子例程中。这是为了安全起见,这样,如果您在其他地方使用同名的标量,就不会出现与意外结果发生冲突的情况。请注意,您需要在文件顶部添加一行use strict
才能强制执行my
。$self
用于面向对象的 Perl。它始终是方法的第一个参数。因此,在像$obj->firstname($foo)
这样的调用中,$obj
将在方法内分配给$self
,并且$foo
将被分配给$firstname
。Since you understand the concept of
@_
you would realise that you could just access these usingshift
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 withuse strict
at the top of your file formy
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
.