它绝对什么都不做……因为 IO::Socket::Unix 已经为你做了。
以块的形式发送数据的开销较小,因此文件库会累积数据以在缓冲区中打印,而不是立即发送到系统。只有当数据已经累积到 4KB 或 8KB(取决于版本)时,才是真正发送到系统的数据。这称为“缓冲”。
将句柄的 autoflush 设置为 true 会禁用该句柄的缓冲。当您调用时,数据在返回print
之前被发送到系统。print
看到不同:
$ perl -e'
STDOUT->autoflush($ARGV[0]);
for (0..9) { print $_ x 1024; sleep 1; }
' 1
<whole bunch of 1s>
<one second later: whole bunch of 2s>
<one second later: whole bunch of 3s>
<one second later: whole bunch of 4s>
<one second later: whole bunch of 5s>
<one second later: whole bunch of 6s>
<one second later: whole bunch of 7s>
<one second later: whole bunch of 8s>
<one second later: whole bunch of 9s>
$ perl -e'
STDOUT->autoflush($ARGV[0]);
for (0..9) { print $_ x 1024; sleep 1; }
' 0
# Before Perl 5.14:
<four seconds later: whole bunch of 0s, 1s, 2s and 3s>
<four seconds later: whole bunch of 4s, 5s, 6s and 7s>
<two seconds later: whole bunch of 8s and 9s>
# Perl 5.14+
<eight seconds later: whole bunch of 0s, 1s, 2s, 3s, 4s, 5s, 6s and 7s>
<two seconds later: whole bunch of 8s and 9s>
autoflush
由 IO::Socket::* 打开,因为它在大多数情况下都需要用于套接字。套接字通常用于交互式通信。请求、回复、请求、回复等。想象一下,如果请求被卡在缓冲区中会发生什么……您将永远等待回复!