Perl:还有比 $var = $_; 更好的东西吗?
这不是重要的问题,我知道,但是 $var = $_;
看起来很蹩脚,是否有更好(更短)的方法来进行该分配?
为了澄清我的问题:我知道我可以在代码中轻松使用 $_
(这就是我喜欢它的原因),但有时我需要存储 $_
并在 < code>$_ 然后返回 $_
的旧值(例如)。
It is not important question and I know it, buuut $var = $_;
looks just lame, is there better (shorter) way to make that assignment?
To clarify my question: I know I can use $_
in code easily (thats why I like it), but sometimes I need to store $_
and do something on $_
and then get back old value of $_
(for example).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
在新的词法范围内,您可以本地化 $_ ,这将防止该范围内的任何修改影响其在该范围之外的值。
为了澄清,有必要举一个例子:
This should print
我发现它对于编写在内部广泛使用 $_ 的函数来说非常有价值,因为我不喜欢它们在周围的范围内破坏 $_ 。但是,您也可以使用它来“搁置”变量并使用它的新版本一段时间。
Within a new lexical scope, you can localise $_ which will prevent any modifications within that scope from affecting its value outside that scope.
An example is necessary for clarification:
This should print
I find it invaluable for writing functions which make extensive use of $_ internally, because I don't like it when they clobber $_ in their surrounding scope. However, you can also use it to 'set aside' a variable and work with a new version of it for a while.
在许多情况下这是不必要的。例如:
或
或
In many circumstances it's unnecessary. For example:
or
or
根据OP的要求,我发布我的评论作为答案。
看起来您是在问是否有更好或更短的方法来编写
$var = $_
(或获得该功能)。对我来说,这是一个相当奇怪的请求,因为:$var = $_
已经是尽可能短了,并且符号。
By the OPs request, I am posting my comment as an answer.
It looks like you are asking if there is a better or shorter way to write
$var = $_
(or get that functionality). To me, that is a rather strange request, because:$var = $_
is already about as short as it gets, andsign.
为什么需要
$var = $_
?只需使用$_
或将其作为参数传递给函数,在函数中调用它$var
。Why do you want
$var = $_
? Just use$_
or pass it in as a parameter to the function, in the function call it$var
.使用
本地
:Use
local
:也许通常名为
apply
的函数就是您正在寻找的。 Apply 就像map
一样,只不过它首先复制其参数:apply {CODE} LIST
这是我的一个模块的实现:
http://search.cpan.org/perldoc?List::Gen#apply
Maybe the function commonly named
apply
is what you are looking for. Apply is just likemap
except it makes a copy of its arguments first:apply {CODE} LIST
Here's an implementation from one of my modules:
http://search.cpan.org/perldoc?List::Gen#apply
所有好的答案。我想再贡献一个与 @awm 所说的“仅使用
$_
”相关的示例。10 分钟前,我刚刚写了这些行:
这是一个 Perl Golf (cit. ),根本不建议使用。
如果您需要将
$_
存储在其他地方并在一段时间后使用它的原始值,您应该执行分配。All good answers. I'd like to contribute with one more example related to "just use
$_
" as @awm said.10 minutes ago I just wrote these lines:
which is a Perl Golf (cit.) , not recommended to use at all.
If you need to store
$_
somewhere else and after some time use it's original value you should perform the assignment.您可以使用
map
通过转换现有数组:You can use
map
to generate a new array by transforming an existing array:您似乎想访问
$_
本地值的下推堆栈。那可能很酷。但是,您可以自己做一些类似的事情。我可以向您展示基础知识。这是一个基于此的简单列表理解:
这是
@comp
:It seems like you would like to access the pushdown stack of the
$_
local values. That could be cool. However, you can do something like this yourself. I can show you the basics.And here is a simple list comprehension based on this:
Here's
@comp
: