IPC::Open3 如何为 CHLD_IN 启用自动刷新

How does IPC::Open3 enable autoflush for CHLD_IN

IPC::Open3 的文档指出:

The CHLD_IN will have autoflush turned on

但是 source code 中没有提到 IO::Handle::autoflush。模块使用什么机制为 CHLD_IN 开启自动刷新?

以下行禁用了缓冲

select((select($handles[0]{parent}), $| = 1)[0]); # unbuffer pipe

可以重写为

my $old_fh = select($handles[0]{parent});
$| = 1;
select($old_fh);

在 Perl 中禁用输出缓冲的传统方法是通过 $| 变量。来自 man perlvar:

  • HANDLE->autoflush( EXPR )
  • $OUTPUT_AUTOFLUSH
  • $|

If set to nonzero, forces a flush right away and after every write or print on the currently selected output channel. Default is 0 (regardless of whether the channel is really buffered by the system or not; $| tells you only whether you've asked Perl explicitly to flush after each write). STDOUT will typically be line buffered if output is to the terminal and block buffered otherwise. Setting this variable is useful primarily when you are outputting to a pipe or socket, such as when you are running a Perl program under rsh and want to see the output as it's happening. This has no effect on input buffering. See getc for that. See select on how to select the output channel. See also IO::Handle.

Mnemonic: when you want your pipes to be piping hot.

设置$|作用于"the currently selected output channel",它是用select.

的单参数形式设置的