Perl 中的 shift() 有何作用?
下面这行可能意味着什么?
my $x = shift;
What could the following line possibly mean?
my $x = shift;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
下面这行可能意味着什么?
my $x = shift;
What could the following line possibly mean?
my $x = shift;
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
shift()
是一个内置的 Perl 子例程,它接受一个数组作为参数,然后返回并删除该数组中的第一项。 通常的做法是通过shift
调用获取传递到子例程的所有参数。 例如,假设您有一个带有三个参数的子例程foo
。 将这些参数分配给局部变量的一种方法是使用shift
,如下所示:这里的困惑在于,shift 似乎没有作为参数传递给数组。 事实上,它是隐式传递“默认”数组的,即子例程内的
@_
或子例程外的@ARGV
。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 withshift
calls. For example, say you have a subroutinefoo
that takes three arguments. One way to get these parameters assigned to local variables is withshift
like so: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.shift
函数从数组中删除第一个元素并返回它。 数组缩短了一个元素。如果您在函数中,则默认数组(如果没有作为参数给出)为
@_
;如果您在文件范围内,则默认数组为@ARGV
。因此在这种情况下
$x
要么被设置为第一个函数参数,要么被设置为第一个命令行参数。The
shift
function removes the first element from an array, and returns it. The array is shortened by one element.The default array (if one isn't given as a parameter) is
@_
if you're in a function, or@ARGV
if you're at file scope.So in this case
$x
is either being set to the first function parameter, or to the first command line parameter.在 Perl 中,如果您没有显式指定参数,许多方法都会使用默认变量(
$_
和@_
)。 您的代码与以下内容相同:正如 PullMonkey 之前指出的,在子例程中,
@_
包含传递给该子例程的参数(如perlsub
)。shift
将从@_
中删除第一个参数值并将其存储在$x
中,因此$_[0]
现在将为您提供传递给子例程的第二个参数。In Perl, many methods use the default variables (
$_
and@_
) if you don't explicitly specify arguments. Your code is identical to:As pointed out by PullMonkey earlier, within a subroutine,
@_
contains the arguments passed to that subroutine (as described inperlsub
).shift
will remove the first argument value from@_
and store it in$x
, so$_[0]
will now give you the second argument passed to your subroutine.这通常是这样的习惯用法:$x 是分配给传递给子例程的第一个参数的局部变量。
可能更清晰(并且它不会修改参数列表)。
This is usually an idiom for: $x is a local variable assigned to the first parameter passed to the subroutine, although.
is probably clearer (and it doesn't modify the argument list).
通俗地说,从非常高层次的角度来看,shift 取数组的第一个元素(最左边的部分),而相反的是 pop 获取数组的最后一个元素(最右边的部分)。
In layman's terms, from a very highlevel view, shift is taking the first element of an array (the leftmost part), while the opposite is pop which is taking the last element of array (the rightmost part).
如果您在子例程中,此行将在
@_
(传入的参数)上shift
。因此,
$x
将是从@_
数组中弹出
的第一个项目。所以通常你会看到
$x = shift if @_;
If you are in a subroutine this line will
shift
on@_
(the params that are passed in).So
$x
would be the first itempopped
from the@_
array.So usually you would see
$x = shift if @_;
阅读
perldoc -f shift
的输出后,您还有疑问吗?(这不是一个问题;这是一个答案;-))
另一个推荐阅读的内容是
man perlbook
。Perl 从
@_
中“移出”第一个元素,并将其返回。因此,在删除
@_
的第一个元素后,$x
被分配给它。@_
本身就是例程参数。因此,这三个是常见的替代方案(但是并不完全相同):
只有第一个变体修改
@_
。Do you still have questions after reading the output of
perldoc -f shift
?(That is not a question; it is an answer ;-))
Another recommended reading is
man perlbook
.Perl "shifts off" the first element from
@_
, returning it.So
$x
is assigned to the first element of@_
after removing it from there.@_
itself are the routine parameters.So these three are common alternatives (not the very same, however):
Only the first variant modifies
@_
.