The Top-of-Page Format
Many reports end up on some hardcopy device, like a laserprinter. Printer paper is generally clipped into page-size chunks, because most of us stopped reading paper in scrolls a long time ago. So the text being fed to a printer typically has to take page boundaries into consideration by putting in blank lines or form-feed characters to skip across the page boundaries. Now, you could take the output of a Perl program and feed it through some utility (maybe even one written in Perl) that does this pagination, but there’s an easier way.
Perl allows you to define a
top-of-page
format that triggers a page-processing mode. Perl counts each line of
output generated by any format invocation to a particular filehandle.
When the next format output cannot fit on the remainder of the
current page, Perl spits out a formfeed followed by an
automatic invocation of the top-of-page format, and finally the text
from the invoked format. In this manner, the result of one
write invocation will never be split across page
boundaries (unless the result is so large that it won’t even
fit on a page by itself).
The top-of-page format is defined just like any other format. The
default name of a top-of-page format for a particular filehandle is
the name of the filehandle followed by
_TOP (in uppercase
only).
Perl defines the variable $% to be the number of times the top-of-page format has been called for a particular filehandle, so you can use this variable in your top-of-page format to ...
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