在Perl中shift()有什么作用? [英] What does shift() do in Perl?
问题描述
以下几行可能意味着什么?
What could the following line possibly mean?
my $x = shift;
推荐答案
shift()
是内置的Perl子例程,该例程将数组作为参数,然后返回并删除该数组中的第一项.通常的做法是使用shift
调用获取传递到子例程中的所有参数.例如,假设您有一个接受三个参数的子例程foo
.将这些参数分配给局部变量的一种方法是使用shift
,如下所示:
shift()
is a built in Perl subroutine that takes an array as an argument, then returns and deletes the first item in that array. It is common practice to obtain all parameters passed into a subroutine with shift
calls. For example, say you have a subroutine foo
that takes three arguments. One way to get these parameters assigned to local variables is with shift
like so:
sub foo() {
my $x = shift;
my $y = shift;
my $z = shift;
# do something
}
这里的混乱之处在于,它似乎没有将shift作为参数传递给数组.实际上,它是隐式传递给默认"数组的,它是子例程内的@_
或子例程外的@ARGV
.
The confusion here is that it appears shift is not being passed an array as an argument. In fact, it is being passed the "default" array implicitly, which is @_
inside a subroutine or @ARGV
outside a subroutine.
这篇关于在Perl中shift()有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!