select (output filehandle) 
select FILEHANDLE
selectFor historical reasons, there are two select operators that are totally unrelated to
each other. (See the next section for the other one.) This version of
the select operator returns the
currently selected output filehandle and, if
FILEHANDLE is supplied, sets the current
default filehandle for output. This has two effects: first, a write or a print without a filehandle will default to
this FILEHANDLE; second, special variables
related to output will refer to this output filehandle. For example, if
you have to set the same top-of-form format for more than one output
filehandle, you might do the following:
select REPORT1; $^ = "MyTop"; select REPORT2; $^ = "MyTop";
But note that this leaves REPORT2 as the currently selected filehandle.
This could be construed as antisocial, since it could really foul up
some other routine’s print or
write statements. Properly written
library routines leave the currently selected filehandle the same on exit as it was on
entry. To support this, FILEHANDLE may be an
expression whose value gives the name of the actual filehandle.
Thus, you can save and restore the currently selected filehandle like
this:
my $oldfh = select STDERR; $| = 1; select $oldfh;
or idiomatically but somewhat obscurely like this:
select((select(STDERR), $| = 1)[0])
This example works by building a list consisting of the returned value ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access