Changing Defaults for Formats
We have often referred to the default for this or that. Well, Perl provides a way to override the defaults for just about every step. Let’s talk about these.
Using select to Change the Filehandle
Back when we talked about print
, in Chapter 6, we mentioned that print
and print
STDOUT
were
identical, because STDOUT
was the default for
print
. Not quite. The real default for
print
(and write
, and a few
other operations we’ll get to in a moment) is an odd notion
called the
currently selected
filehandle.
The currently selected filehandle starts out as
STDOUT
—which makes it easy to print things
on the standard output. However, you can change the currently
selected filehandle with the
select
function. This function takes a single filehandle (or a scalar
variable containing the name of a filehandle) as an argument. After
the currently selected filehandle is changed, it affects all future
operations that depend on the currently selected filehandle. For
example:
print "hello world\n"; # like print STDOUT "hello world\n"; select (LOGFILE); # select a new filehandle print "howdy, world\n"; # like print LOGFILE "howdy, world\n"; print "more for the log\n"; # more for LOGFILE select (STDOUT); # re-select STDOUT print "back to stdout\n"; # this goes to standard output
Note that the select
operation is
sticky—after you’ve selected a new handle, it stays in
effect until the next select
.
So, a better definition for STDOUT
with respect to
print
and write
is that
STDOUT
is ...
Get Learning Perl on Win32 Systems now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.