$ _与命名的输入或循环参数有何不同? [英] How is $_ different from named input or loop arguments?

查看:66
本文介绍了$ _与命名的输入或循环参数有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我经常使用$ _,所以我想更好地了解其用法.据我了解和使用,$ _是隐式值的全局变量.

As I use $_ a lot I want to understand its usage better. $_ is a global variable for implicit values as far as I understood and used it.

由于$ _似乎已经设置,是否有理由在$ _ 以外可读性上使用命名循环变量?

As $_ seems to be set anyway, are there reasons to use named loop variables over $_ besides readability?

在什么情况下,$ _是全局变量有什么用?

In what cases does it matter $_ is a global variable?

所以,如果我使用

for (@array){
    print $_;
}

甚至

print $_ for @array;

for my $var (@array){
    print $var;
}

但是它的作用相同吗?我想那不完全是,但实际区别是什么?

But does it work the same? I guess it does not exactly but what are the actual differences?

更新:

在此示例中,似乎$ _的范围也正确.不再是全球性的了吗?我正在使用5.12.3.

It seems $_ is even scoped correctly in this example. Is it not global anymore? I am using 5.12.3.

#!/usr/bin/perl
use strict;
use warnings;

my @array = qw/one two three four/;
my @other_array = qw/1 2 3 4/;

for (@array){
    for (@other_array){
        print $_;
    }
    print $_;
}

可以正确打印1234one1234two1234three1234four.

that prints correctly 1234one1234two1234three1234four.

对于全局$ _,我期望1234 4 1234 4 1234 4 1234 4 ..或我缺少明显的东西吗?

For global $_ I would have expected 1234 4 1234 4 1234 4 1234 4 .. or am i missing something obvious?

那么$ _什么时候是全局的?

When is $_ global then?

更新:

好吧,在仔细阅读了各种答案和perlsyn之后,我得出了一个结论:

Ok, after having read the various answers and perlsyn more carefully I came to a conclusion:

除可读性外,最好避免使用$ _,因为必须知道$ _的隐式本地化,并考虑到否则可能会遇到意外的行为.

Besides readability it is better to avoid using $_ because implicit localisation of $_ must be known and taken account of otherwise one might encounter unexpected behaviour.

感谢您对此事进行澄清.

Thanks for clarification of that matter.

推荐答案

are there reasons to use named loop variables over $_ besides readability?

问题是不是它们是否被命名.问题是它们是包变量"还是词法变量".

The issue is not if they are named or not. The issue is if they are "package variables" or "lexical variables".

请参阅Perl应对范围"中使用的两个变量系统的很好描述:

See the very good description of the 2 systems of variables used in Perl "Coping with Scoping":

http://perl.plover.com/FAQs/Namespaces.html

包装变量是全局变量,因此应出于所有常见原因(例如,远距离采取行动)避免使用.

package variables are global variables, and should therefore be avoided for all the usual reasons (eg. action at a distance).

避免包变量是正确操作"或难以注入错误"的问题,而不是可读性"的问题.

Avoiding package variables is a question of "correct operation" or "harder to inject bugs" rather than a question of "readability".

In what cases does it matter $_ is a global variable?

无处不在.

更好的问题是:

In what cases is $_ local()ized for me?

在一些地方,Perl会为您本地化$ _,主要是foreach,grep和map.所有其他地方都要求您自己对它进行local()化,因此,当您不可避免地忘记这样做时,您将在注入一个潜在的错误. :-)

There are a few places where Perl will local()ize $_ for you, primarily foreach, grep and map. All other places require that you local()ize it yourself, therefore you will be injecting a potential bug when you inevitably forget to do so. :-)

这篇关于$ _与命名的输入或循环参数有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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