Perl 5.10+中词汇$ _的好,坏和丑陋 [英] The good, the bad, and the ugly of lexical $_ in Perl 5.10+

查看:106
本文介绍了Perl 5.10+中词汇$ _的好,坏和丑陋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Perl 5.10开始,现在可以对上下文变量$_进行词汇范围化,可以显式地定义为my $_;或在given / when构造中.

Starting in Perl 5.10, it is now possible to lexically scope the context variable $_, either explicitly as my $_; or in a given / when construct.

有人发现词汇$_的用法很好吗?它会使任何构造更简单/更安全/更快吗?

Has anyone found good uses of the lexical $_? Does it make any constructs simpler / safer / faster?

那使情况变得更加复杂的情况又如何呢?词法$_是否在您的代码中引入了任何错误? (由于写入$_的控制结构将在其作用域内使用词法版本,因此如果它包含任何子例程调用(由于失去动态作用域,则可以更改代码的行为))

What about situations that it makes more complicated? Has the lexical $_ introduced any bugs into your code? (since control structures that write to $_ will use the lexical version if it is in scope, this can change the behavior of the code if it contains any subroutine calls (due to loss of dynamic scope))

最后,我想构建一个列表,以阐明何时将$_用作词法,用作全局变量,或者什么时候都不重要.

In the end, I'd like to construct a list that clarifies when to use $_ as a lexical, as a global, or when it doesn't matter at all.

注意::自perl5-5.24起,这些实验性功能为

NB: as of perl5-5.24 these experimental features are no longer part of perl.

推荐答案

IMO,从词法$_中出现的一件很棒的事情是新的_原型符号.

IMO, one great thing to come out of lexical $_ is the new _ prototype symbol.

这使您可以指定一个子例程,以便使用一个标量,或者如果不提供该子例程,它将获取$_.

This allows you to specify a subroutine so that it will take one scalar or if none is provided it will grab $_.

所以不要写:

sub foo {
    my $arg = @_ ? shift : $_;

    # Do stuff with $_
}

我可以写:

sub foo(_) {
    my $arg = shift;

    # Do stuff with $_ or first arg.
}

没什么大变化,但是当我想要这种行为时,它就简单得多了.去除样板是一件好事.

Not a big change, but it's just that much simpler when I want that behavior. Boilerplate removal is a good thing.

当然,这具有更改多个内置插件(例如chr)的原型的连锁反应,这可能会破坏一些代码.

Of course, this has the knock on effect of changing the prototypes of several builtins (eg chr), which may break some code.

总体而言,我欢迎词法$_.它为我提供了一种工具,可以用来限制意外的数据处理和功能之间的怪异互动.如果我决定在函数的主体中使用$_,通过对它进行词汇化处理,我可以确定无论我叫什么代码,$_都不会在调用代码中被修改.

Overall, I welcome lexical $_. It gives me a tool I can use to limit accidental data munging and bizarre interactions between functions. If I decide to use $_ in the body of a function, by lexicalizing it, I can be sure that whatever code I call, $_ won't be modified in calling code.

动态范围很有趣,但是在大多数情况下,我都希望进行词法作用域.加上$_的复杂性.我听过关于仅做local $_;的不明智建议的可怕警告-最好改用for ( $foo ) { }.当我以任何方式对$_进行本地化时,词汇化的$_给我想要的结果是100的99倍.词法$_使强大的便利性和可读性变得更加强大.

Dynamic scope is interesting, but for the most part I want lexical scoping. Add to this the complications around $_. I've heard dire warnings about the inadvisability of simply doing local $_;--that it is best to use for ( $foo ) { } instead. Lexicalized $_ gives me what I want 99 times out of 100 when I have localized $_ by whatever means. Lexical $_ makes a great convenience and readability feature more robust.

我的大部分工作必须使用perl 5.8,所以我没有在大型项目中使用词法$_的乐趣.但是,感觉这将大大提高使用$_的安全性,这是一件好事.

The bulk of my work has had to work with perl 5.8, so I haven't had the joy of playing with lexical $_ in larger projects. However, it feels like this will go a long way to make the use of $_ safer, which is a good thing.

这篇关于Perl 5.10+中词汇$ _的好,坏和丑陋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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