我的 $_ 吗?如果隐含 $_ 则执行任何操作

发布于 2024-09-12 12:54:52 字数 446 浏览 6 评论 0原文

我认为答案是肯定的,但我只是想确定一下。那么,如果我有

sub something {
    my $_;
    my @array = ...;
    while ( @array ) {
        say;
    }
}

my $_; 实际上可以有效地词汇化传递给 say 的参数吗?

在这种特殊情况下,我使用 DZP::UnusedVarsTests 并抱怨我没有使用 my $_; 并且我怀疑这是一个错误,因为我在隐含的情况下使用它。

I think the answer is yes but I just want to make sure. so if I have

sub something {
    my $_;
    my @array = ...;
    while ( @array ) {
        say;
    }
}

is the my $_; actually effective at lexicalizing the parameter passed to the say?

In this particular case I'm using the DZP::UnusedVarsTests and it's complaining that I haven't used my $_; and I suspect it's a bug since I'm using it in a case where it's implied.

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

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

发布评论

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

评论(1

缱绻入梦 2024-09-19 12:54:52

简短的回答是肯定的。它使该作用域中的函数使用词法作用域 $_,而不是全局 $_。如果他们写回 $_,就像 s/// 的情况一样,您将获得一定程度的损害控制。

根据 perldoc perldelta (5.10.0)

List::Util::first”在存在词法 $_ 时行为不当(通常由“my$_”或隐式地通过“given”)。每次迭代设置的变量是包变量 $_,而不是词法 $_ [RT #67694]。

类似的问题可能会出现在提供将块作为第一个参数的函数的其他模块中,例如

foo { ... $_ ...} 列表

perldoc perl591delta 它接着说:

词法 $_

默认变量 $_ 现在可以通过如下声明来进行词法化:
任何其他词汇变量,带有
简单

<前><代码>我的$_;

$_ 上默认的操作将使用 $_ 存在的词法范围版本,而不是全局 $_ >.

在“map”或“grep”块中,如果$_以前是my' ed,那么块内的 $_ 也是词法的(并且作用域为块)。

$_ 已词汇化的范围内,您仍然可以使用 $::_ 访问 $_ 的全局版本code>,或者更简单地说,用“our $_”覆盖词法声明。

示例

我想提供一些示例来说明为什么要使用此功能:

my $_ = 'BOOM!';

sub something {
    my $_;                         ## Try running with and without
    my @array = qw/foo bar baz/;
    while ( $_ = pop @array ) {
        say;
    }   
}   

something();

say;

并且,另一个示例

my $_ = 'foo';

sub something {
  my $_ = $_;  ## Try running with and without
  s/foo/bar/;
  $_;
}

something();

say;

The short answer is Yes. It makes the functions in that scope use the lexically scoped $_, and not the global $_. If they write back to $_, as in the case of s///, you will have some level of damage control.

Per perldoc perldelta (5.10.0):

"List::Util::first" misbehaves in the presence of a lexical $_ (typically introduced by "my $_" or implicitly by "given"). The variable which gets set for each iteration is the package variable $_, not the lexical $_ [RT #67694].

A similar issue may occur in other modules that provide functions which take a block as their first argument, like

foo { ... $_ ...} list

And, in perldoc perl591delta it goes on to say:

Lexical $_

The default variable $_ can now be lexicalized, by declaring it like
any other lexical variable, with a
simple

     my $_;

The operations that default on $_ will use the lexically-scoped version of $_ when it exists, instead of the global $_.

In a "map" or a "grep" block, if $_ was previously my'ed, then the $_ inside the block is lexical as well (and scoped to the block).

In a scope where $_ has been lexicalized, you can still have access to the global version of $_ by using $::_, or, more simply, by overriding the lexical declaration with "our $_".

Examples

I wanted to provide some examples of why this functionality would be used:

my $_ = 'BOOM!';

sub something {
    my $_;                         ## Try running with and without
    my @array = qw/foo bar baz/;
    while ( $_ = pop @array ) {
        say;
    }   
}   

something();

say;

And, another example

my $_ = 'foo';

sub something {
  my $_ = $_;  ## Try running with and without
  s/foo/bar/;
  $_;
}

something();

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