It does absolutely nothing ...because IO::Socket::Unix already does it for you.
There's less overhead to send data in chunks, so file libraries accumulate data to print in a buffer instead of sending to the system immediately. Only when 4KB or 8KB (depending on the version) of data has been accumulated is the data actually sent to the system. This is called "buffering".
Setting autoflush to true for a handle disables buffering for that handle. When you call print
, the data is sent to the system before print
returns.
See the difference:
$ 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
is turned on by IO::Socket::* because it's needed most of the times for sockets. Sockets are often used for interactive communication. Request, reply, request, reply, etc. Imagine what would happen if the request was stuck in a buffer.... You'd be waiting for the reply forever!
$sock->autoflush(1);
,STDOUT->autoflush(1);
, etc – Restorative