Perl,移位if(@_)

时间:2014-06-02 18:47:41

标签: perl

有人可以告诉我什么

shift if(@_)

意味着在perl?

等等

sub id {
   my $self = shift;
   $self->{ID} = shift if @_;

   return $self->{ID};
}

意思?感谢。

5 个答案:

答案 0 :(得分:5)

这是对象的手动存取器/变换器

print $obj->id;       # Accessor form
$obj->id(NEW_VAL);    # Mutator form

它在功能上等同于:

sub id {
    my $self = shift;
    if (@_) { # If called with additional parameters, set the value:
         $self->{ID} = shift(@_);
    }
    return $self->{ID};
}

答案 1 :(得分:4)

在sub中,shift不带参数使用@_数组。

所以

$self->{ID} = shift if @_;

等于

$self->{ID} = shift(@_) if @_;

(从@_数组中移除最左边的元素并将其分配给$self->{ID}

答案 2 :(得分:1)

shift从数组中取出第一个元素并返回它。如果没有给出数组,它将在@_上运行,该数组包含函数参数。 if @_语句修饰符仅在@_至少包含一个元素时才会执行前面的语句。

在您的示例中,$self->{ID} = shift @_ if @_;表示"如果有函数参数,请将其指定给$self->{ID}

答案 3 :(得分:1)

shift if(@_)说“如果@_有任何元素(即,在标量上下文中评估@_大于零),则移出默认参数的第一个元素(在子例程内) ,这是@_)“。

sub是标准的前Moose setter / getter方法。评论说:

sub id {
    my $self = shift;   # @_ is the implied argument of shift.
                        # Since method calls prepend the object reference to @_,
                        # this grabs the object itself, assumed by the later code
                        # to be a hash reference.

    $self->{ID} = shift if @_;
                        # If there's still anything left in @_ (`if @_`), get
                        # the first item and stash it under the ID key in 
                        # the hash referenced by $self (note that if there is more
                        # than one item, we'll only stash the first one).

    return $self->{ID}; # Return whatever the value of the item stored under ID in the
                        # hash referenced by $self is. This will be the value just 
                        # assigned if the method was called with a scalar argument, 
                        # or whatever value was there before if no argument was passed.
                        # This will be undef if nothing was ever stored under the ID key.
}

调用

$obj->id();

获取,

$obj->id($value);

设置。

标准的前Moose构造函数会创建一个匿名哈希引用,bless它将它转换为一个对象(将实现该类行为的包连接到该引用),然后返回它:

sub new {
    my($class) = @_;     # List assignment to a list; puts first item in one into the first
                         # item in another; a call to a class method prepends the package
                         # (class) name to @_, so this gets us the name of the class this
                         # object is to belong to.
    my $self = {};       # Gets a new anonymous hash reference into $self.
    bless $self, $class; # Connects the hash reference to the package, so that method calls
                         # made on this object are directed to this class (and its @ISA
                         # ancestors) to find the sub to be called to implement the method.
    return $self;        # Hands the object back to the caller.
}

答案 4 :(得分:0)

您的示例中没有shift if @_ - if适用于整个语句,而不仅仅是shift表达式(因为if在表达式中的优先级非常低层次)。

$self->{ID} = shift if @_;

简称:

if (@_) {
    $self->{ID} = shift;
}
没有参数的

shift将第一个元素从@_列表中拉出,所以如果有任何参数,这会将$self->{ID}设置为子例程的第一个参数。

有关if的后缀使用的一般规则是:

<expression1> if <expression2>;

简称:

if (<expression2>) {
    <expression1>;
}