11.6 Changing Defaults for FormatsWe 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. 11.6.1 Using select to Change the Filehandle
Back when we talked about
The currently selected filehandle starts out as 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
So, a better definition for
Subroutines may find a need to change the currently selected filehandle. However, you would be shocked if you called a subroutine and then found out that all of your carefully crafted text lines were going into some bit bucket because the subroutine changed the currently selected filehandle without restoring it. So what's a well-behaved subroutine to do? If the subroutine knows that the current handle is
The
return value from $oldhandle = select LOGFILE; print "this goes to LOGFILE\n"; select ($oldhandle); # restore the previous handle
Yes, for these examples, putting 11.6.2 Changing the Format Name
The
default format name for a particular filehandle is the same as the filehandle. However, you can change this name for the currently selected filehandle by setting the new format name into a special variable called
For example, to use the $~ = "ADDRESSLABEL";
But what if you want to set the format for the $oldhandle = select REPORT; $~ = "SUMMARY"; select ($oldhandle); The next time we say: write (REPORT);
we get text out on the
Note that we saved the previous handle into a scalar variable and then restored it later. This maneuver is good programming practice. In fact, in production code, we probably would have handled the previous one-line example similarly and not assumed that By setting the current format for a particular filehandle, you can interleave many different formats in a single report. 11.6.3 Changing the Top-of-Page Format Name
Just as we can change the name of the format for a particular filehandle by setting the 11.6.4 Changing the Page Length
If a top-of-page format is defined, the
page length becomes important. By default, the page length is 60 lines; that is, when a
Sometimes 60 lines isn't right. You can change this by setting the $old = select LOGFILE; # select LOGFILE and save old handle $= = 30; select $old;
Changing the page length won't have any effect until the next time the top-of-page format is invoked. If you set it before any text is output to a filehandle through a format, it'll work just fine because the top-of-page format is invoked immediately at the first 11.6.5 Changing the Position on the Page
If you
For example, to tell Perl that you've sent an extra line to write; # invoke STDOUT format on STDOUT ...; print "An extra line... oops!\n"; # this goes to STDOUT $- --; # decrement $- to indicate non-write line went to STDOUT ...; write; # this will still work, taking extra line into account
At the beginning of the program, |
|