ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
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 FilehandleBack 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, it would be shocking to call a subroutine and then find 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 Well it turns out that the return value from $oldhandle = select LOGFILE; print "this goes to LOGFILE\n"; select ($oldhandle); # restore the previous handle Yes, for these examples, it's much easier simply to put 11.6.2 Changing the Format NameThe default format name for a particular filehandle is the same as the filehandle. However, you can change this for the currently selected filehandle by setting the new format name to 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 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 NameJust as we can change the name of the format for a particular filehandle by setting the 11.6.4 Changing the Page LengthIf 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 PageIf 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, |