Perl 中的 shift() 有何作用?

发布于 2024-07-09 04:57:09 字数 60 浏览 4 评论 0原文

下面这行可能意味着什么?

my $x = shift;

What could the following line possibly mean?

my $x = shift;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

紫南 2024-07-16 04:57:09

shift() 是一个内置的 Perl 子例程,它接受一个数组作为参数,然后返回并删除该数组中的第一项。 通常的做法是通过 shift 调用获取传递到子例程的所有参数。 例如,假设您有一个带有三个参数的子例程 foo。 将这些参数分配给局部变量的一种方法是使用 shift ,如下所示:

sub foo() {
  my $x = shift;
  my $y = shift;
  my $z = shift;
  # do something
}

这里的困惑在于,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 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
}

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.

岁吢 2024-07-16 04:57:09

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.

温暖的光 2024-07-16 04:57:09

在 Perl 中,如果您没有显式指定参数,许多方法都会使用默认变量($_@_)。 您的代码与以下内容相同:

my $x = shift @_;

正如 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:

my $x = shift @_;

As pointed out by PullMonkey earlier, within a subroutine, @_ contains the arguments passed to that subroutine (as described in perlsub). 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.

蝶…霜飞 2024-07-16 04:57:09

这通常是这样的习惯用法:$x 是分配给传递给子例程的第一个参数的局部变量。

my ($x) = @_;

可能更清晰(并且它不会修改参数列表)。

This is usually an idiom for: $x is a local variable assigned to the first parameter passed to the subroutine, although.

my ($x) = @_;

is probably clearer (and it doesn't modify the argument list).

久夏青 2024-07-16 04:57:09

通俗地说,从非常高层次的角度来看,shift 取数组的第一个元素(最左边的部分),而相反的是 pop 获取数组的最后一个元素(最右边的部分)。

my @array1=(5,6,7,8,9);
my $x = shift @array1;
print "$x\n"; # 5
print "@array1\n"; # 6 7 8 9

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).

my @array1=(5,6,7,8,9);
my $x = shift @array1;
print "$x\n"; # 5
print "@array1\n"; # 6 7 8 9
寄意 2024-07-16 04:57:09

如果您在子例程中,此行将在 @_(传入的参数)上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 item popped from the @_ array.

So usually you would see $x = shift if @_;

甜味超标? 2024-07-16 04:57:09

阅读 perldoc -f shift 的输出后,您还有疑问吗?

(这不是一个问题;这是一个答案;-))

另一个推荐阅读的内容是 man perlbook

Perl 从 @_ 中“移出”第一个元素,并将其返回。
因此,在删除 @_ 的第一个元素后,$x 被分配给它。
@_ 本身就是例程参数。

因此,这三个是常见的替代方案(但是并不完全相同):

my $a = shift;
my $b = shift;
my $c = shift;
my ($a, $b, $c) = @_;
my $a = $_[0];
my $b = $_[1];
my $c = $_[2];

只有第一个变体修改 @_

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):

my $a = shift;
my $b = shift;
my $c = shift;
my ($a, $b, $c) = @_;
my $a = $_[0];
my $b = $_[1];
my $c = $_[2];

Only the first variant modifies @_.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文