我的自我$ =在Perl转变;一个解释 [英] my $self = shift in Perl; an explanation

查看:246
本文介绍了我的自我$ =在Perl转变;一个解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个真的很难理解OO的Perl的交集和我的$自我=移位; 这些单个元素的文档是伟大的,但他们都不我发现他们是如何协同工作的触摸。

I'm having a really hard time understanding the intersection of OO Perl and my $self = shift; The documentation on these individual elements is great, but none of them that I've found touch on how they work together.

我一直在使用穆斯使具有属性的模块,当然它的内部引用一个模块的属性是非常有用的说模块。有人告诉我一遍又一遍地使用我的$自我=移位; 一个子程序中的模块的属性分配给该变量。这是有道理的和作品,但是当我还在传递参数给子程序,这个过程显然需要在 @ARGV 数组的第一个元素,并将其分配给 $自为好。

I've been using Moose to make modules with attributes, and of course it's useful to reference a module's attribute within said module. I've been told over and over again to use my $self = shift; within a subroutine to assign the module's attributes to that variable. This makes sense and works, but when I'm also passing arguments to the subroutine, this process clearly takes the first element of the @ARGV array and assigns it to $self as well.

有人能提供我怎么可以用shift来获得一个模块的属性内部访问,同时还通过在 @ARGV 数组中的参数解释?

Can someone offer an explanation of how I can use shift to gain internal access to a module's attributes, while also passing in arguments in the @ARGV array?

推荐答案

首先,一个子程序没有通​​过 @ARGV 阵列。而所有传递给子程序的参数被压扁成 psented一个列表重新$ P $ @ _ 子程序中。在@ARGV数组可在脚本的顶层,包含传递给你的命令行参数的脚本。

First off, a subroutine isn't passed the @ARGV array. Rather all the parameters passed to a subroutine are flattened into a single list represented by @_ inside the subroutine. The @ARGV array is available at the top-level of your script, containing the command line arguments passed to you script.

现在,在Perl中,当你调用一个方法的对象,该对象被隐含作为参数传递给该方法。

Now, in Perl, when you call a method on an object, the object is implicitly passed as a parameter to the method.

所以这个:

 $obj->doCoolStuff($a, $b);

等同于这样的:

 doCoolStuff($obj, $a, $b);

这意味着在方法 @_ 的内容 doCoolStuff 将是:
     @_ =($ OBJ,$ A,$ B);

现在,在内置函数,不带任何参数,移动元素了默认的数组变量 @_ 。在这种情况下,这将是 $ OBJ

Now, the shift builtin function, without any parameters, shifts an element out of the default array variable @_. In this case, that would be $obj.

所以,当你做 $自=移时,实际上是说 $自我= $ OBJ

So when you do $self = shift, you are effectively saying $self = $obj.

我也希望这解释了如何将其​​他参数传递到通过的方法 - > 符号。继续我上面所陈述的例子,这将是这样的:

I also hope this explains how to pass other parameters to a method via the -> notation. Continuing the example I've stated above, this would be like:

sub doCoolStuff {
  # Remember @_ = ($obj, $a, $b)
  my $self = shift;
  my ($a, $b) = @_;

此外,而穆斯是Perl的一个伟大的对象层,它不会从你需要初始化 $要求带走自自己在每个方法。一定要记住这一点。虽然像C ++和Java语言初始化对象引用这个含蓄,在Perl中你需要为每一个你写的方法做明确。

Additionally, while Moose is a great object layer for Perl, it doesn't take away from the requirement that you need to initialize the $self yourself in each method. Always remember this. While language like C++ and Java initialize the object reference this implicitly, in Perl you need to do it explicitly for every method you write.

这篇关于我的自我$ =在Perl转变;一个解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆