new_perl
new_perl

Reputation: 7735

What's the purpose of such code?

253:        my $sel = select(FOUT);
254:        $| = 1;                             # for DB::OUT
255:        select($sel);

Looks really weird to me,spotted in Term::ReadLine module.

Upvotes: 5

Views: 202

Answers (2)

Alexandr Ciornii
Alexandr Ciornii

Reputation: 7394

it's an old form of

use IO::Handle;
FOUT->autoflush(1);

Upvotes: 0

Steve-o
Steve-o

Reputation: 12866

Writing to STDOUT (or any other output filehandle) is buffered by default. To ask Perl to flush immediately after each write or print command, set the special variable $| to 1.

http://www.perlhowto.com/disable_output_buffering

edit: for further explanation:

my $sel = select(FOUT);

FOUT is a file handle, using select makes it the default file handle so that any operation using the default file handle will now use FOUT. For example print "moo" will be the equivalent to print FOUT "moo".

The return value of select is the previous default file handle, i.e. standard output.

$| = 1;

This command disables output buffering on the default file handle, as the handle is FOUT is disables output buffering for FOUT.

select($sel);

Now we bring back the previous default file handle, i.e. standard output, so print commands, etc, work as expected.

edit #2: further explanation of file handles:

Imagine you have a series of file handles, STDOUT, FILE_ONE, FILE_TWO, SOCKET_ONE, and SOCKET_TWO. You want to set FILE_ONE and SOCKET_TWO to have no output buffering.

# On startup Perl effectively does the following:
# select(STDOUT);
my $sel = select(FILE_ONE);
# $sel is now STDOUT
$| = 1;
select(SOCKET_TWO);
$| = 1;
# bring back STDOUT
select($sel);

Now lets discover what happens with that magical default file handle.

print "HELLO\n";
# equivalent to: print STDOUT "HELLO\n";
my $sel = select(FILE_ONE);
# sets `default file handle` to FILE_ONE
print "HELLO\n";
# equivalent to: print FILE_ONE "HELLO\n";
$| = 1;
# disables output buffering on handle FILE_ONE
select(SOCKET_TWO)
# sets `default file handle` to SOCKET_TWO
print "HELLO\n";
# equivalent to: print SOCKET_TWO "HELLO\n";
$| = 1;
# disables output buffering on handle SOCKET+TWO
select($sel);
# sets `default file handle` to STDOUT

Alternatively lets invent some a new variable:

$FH
# let this be the `default file handle`

Lets invent a new function:

sub disable_output_buffer ($file_handle) {
# magic occurs here
}

Now lets rewrite the previous code using this new file handle and function.

# print "HELLO\n";
my $FH = STDOUT;
print $FH "HELLO\n"           # print STDOUT "HELLO\n"

# my $sel = select(FILE_ONE);
my $sel = $FH;
$FH = FILE_ONE;

# print "HELLO\n";
print $FH "HELLO\n";          # print FILE_ONE "HELLO\n"

# $| = 1
disable_output_buffer ($FH);  # disable_output_buffer (FILE_ONE)

# select(SOCKET_TWO);
$FH = SOCKET_TWO;

# print "HELLO\n";
print $FH "HELLO\n";          # print SOCKET_TWO "HELLO\n"

# $| = 1
disable_output_buffer ($FH);  # disable_output_buffer (SOCKET_TWO)

# select($sel);
$FH = $sel;

Upvotes: 8

Related Questions