Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of such code?

Tags:

perl

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

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

like image 634
new_perl Avatar asked Jul 29 '11 04:07

new_perl


People also ask

What is a purpose of a code?

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.

What is the purpose of learning to code?

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.

What are the 4 types of code?

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.


1 Answers

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;
like image 163
Steve-o Avatar answered Oct 14 '22 15:10

Steve-o