253: my $sel = select(FOUT);
254: $| = 1; # for DB::OUT
255: select($sel);
Looks really weird to me,spotted in Term::ReadLine
module.
Coding creates a set of instructions for computers to follow. These instructions determine what actions a computer can and cannot take. Coding allows programmers to build programs, such as websites and apps. Computer programmers can also tell computers how to process data in better, faster ways.
It can enhance problem-solving skills Coding can help you improve your logical thinking skills by allowing you to see problems from a new perspective. Complex coding projects are made up of smaller tasks and by breaking down the problem and adopting a methodological way of thinking, you can tackle any challenge.
There are hundreds of coding languages in existence today. While the names of the coding paradigms sometimes vary, most experts agree on four primary types of code: imperative, functional, logical, and object-oriented.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With