-$ |在Perl工作? [英] How does --$| work in Perl?

查看:72
本文介绍了-$ |在Perl工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我遇到了这种方法,用于过滤列表的第二个值:

Recently I came across this way to filter out every second value of a list:

perl -E 'say grep --$|, 1..10'
13579

它如何工作?

推荐答案

$|是一个特殊的Perl变量,只能具有值0和1. 对$| true 非零值的任何赋值,例如

$| is a special Perl variable that can only have the values 0 and 1. Any assignment to $| of a true non-zero value, like


$| = 1;
$| = 'foo';
$| = "4asdf";          # 0 + "4asdf" is 4
$| = \@a;

将具有将$|设置为1的效果. false 零值的任何赋值

will have the effect of setting $| to 1. Any assignment of a false zero value


$| = 0;
$| = "";
$| = undef;
$| = "erk";            # 0 + "erk" is 0

会将$|设置为0.

--$|扩展到$| = $| - 1,现在您可以看到发生了什么. 如果$|最初为1,则--$|会将值更改为0. 如果$|最初为0,则--$|会尝试将值设置为-1,但实际上会将值设置为1.

Expand --$| to $| = $| - 1, and now you can see what is going on. If $| was originally 1, then --$| will change the value to 0. If $| was originally 0, then --$| will try to set the value to -1 but will actually set the value to 1.

这篇关于-$ |在Perl工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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