"select((select(s),$ | = 1)[0]))"是什么意思?在Perl做? [英] What does "select((select(s),$|=1)[0])" do in Perl?
问题描述
我已经看到了用Perl编写的一些可怕的代码,但是我无法做到这一点:
I've seen some horrific code written in Perl, but I can't make head nor tail of this one:
select((select(s),$|=1)[0])
这是我们用来与服务器通信的一些网络代码,我认为这与缓冲有关(因为它设置了$|
).
It's in some networking code that we use to communicate with a server and I assume it's something to do with buffering (since it sets $|
).
但是我不知道为什么有多个select
调用或数组引用.有人可以帮我吗?
But I can't figure out why there's multiple select
calls or the array reference. Can anyone help me out?
推荐答案
这是一个讨厌的小习惯,用于在STDOUT以外的文件句柄上设置自动刷新.
It's a nasty little idiom for setting autoflush on a filehandle other than STDOUT.
select()
接受提供的文件句柄,并(基本上)用它替换STDOUT,并在完成后返回旧的文件句柄.
select()
takes the supplied filehandle and (basically) replaces STDOUT with it, and it returns the old filehandle when it's done.
因此,(select($s),$|=1)
重定向文件句柄(记住select
返回旧的文件句柄),并设置自动刷新($| = 1
).它在列表((...)[0]
)中执行此操作,并返回第一个值(这是select
调用的结果-原始STDOUT),然后将 that 传递回另一个select
恢复原始的STDOUT文件句柄. ew.
So (select($s),$|=1)
redirects the filehandle (remember select
returns the old one), and sets autoflush ($| = 1
). It does this in a list ((...)[0]
) and returns the first value (which is the result of the select
call - the original STDOUT), and then passes that back into another select
to reinstate the original STDOUT filehandle. Phew.
但是现在您了解了(嗯,也许;)),请改为执行此操作:
But now you understand it (well, maybe ;)), do this instead:
use IO::Handle;
$fh->autoflush;
这篇关于"select((select(s),$ | = 1)[0]))"是什么意思?在Perl做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!