在 Perl 中 shift() 有什么作用? [英] What does shift() do in Perl?

查看:30
本文介绍了在 Perl 中 shift() 有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面这行可能是什么意思?

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屋!

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