“select((select(s),$|=1)[0])"是什么意思?在 Perl 中做什么? [英] What does "select((select(s),$|=1)[0])" do in Perl?

查看:32
本文介绍了“select((select(s),$|=1)[0])"是什么意思?在 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),然后将 那个 传回另一个 select 以恢复原始 STDOUT 文件句柄.呼.

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屋!

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