2.8 Formats
Perl has a mechanism to help you generate simple, formatted reports
and charts. It can keep track of things like how many lines on a
page, what page you're on, when to print page headers, and so on.
Keywords are borrowed from FORTRAN:
format
to declare and
write
to execute; see the relevant entries in
Chapter 3
. Fortunately, the layout is much more legible,
more like the
Formats, like packages and subroutines, are declared rather than
executed, so they may occur at any point in your program. (Usually
it's best to keep them all together.) They have their own namespace
apart from all the other types in Perl. This means that if you have a
function named " Output record formats are declared as follows:
format
If
Picture lines are printed exactly as they look, except for certain fields
that substitute values into the line.[
48
]
Each substitution field in a picture line starts
with either
As an alternate form of right justification, you may also use The values are specified on the following line in the same order as the picture fields. The expressions providing the values should be separated by commas. The expressions are all evaluated in a list context before the line is processed, so a single list expression could produce multiple list elements. The expressions may be spread out to more than one line if enclosed in braces. If so, the opening brace must be the first token on the first line.
Picture fields that begin with
Using ^ fields can produce variable-length records. If the text to
be formatted is short, just repeat the format line with the ^ field
in it a few times. If you just do this for short data you'd end
up getting a few blank lines. To suppress lines that would end up blank,
put a
Top-of-form processing is by default handled by a format with the same
name as the current filehandle with " Examples:
# a report on the /etc/passwd file format STDOUT_TOP = Passwd File Name Login Office Uid Gid Home ------------------------------------------------------------------ . format STDOUT = @<<<<<<<<<<<<<<<<<< @||||||| @<<<<<<@>>>> @>>>> @<<<<<<<<<<<<<<<<< $name, $login, $office,$uid,$gid, $home . # a report from a bug report form format STDOUT_TOP = Bug Reports @<<<<<<<<<<<<<<<<<<<<<<< @||| @>>>>>>>>>>>>>>>>>>>>>>> $system, $%, $date ------------------------------------------------------------------ . format STDOUT = Subject: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $subject Index: @<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $index, $description Priority: @<<<<<<<<<< Date: @<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $priority, $date, $description From: @<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $from, $description Assigned to: @<<<<<<<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $programmer, $description ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $description ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $description ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $description ~ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<< $description ~ ^<<<<<<<<<<<<<<<<<<<<<<<... $description .
It is possible to intermix
print
s with
write
s on the same output channel, but
you'll have to handle the
$-
special
variable ( 2.8.1 Format Variables
The current format name is stored in the variable
$~
(
select((select(OUTF), $~ = "My_Other_Format", $^ = "My_Top_Format" )[0]); Pretty ugly, eh? It's a common idiom though, so don't be too surprised when you see it. You can at least use a temporary variable to hold the previous filehandle (this is a much better approach in general, because not only does legibility improve, but you now have an intermediary statement in the code to stop on when you're single-stepping the debugger):
$ofh = select(OUTF); $~ = "My_Other_Format"; $^ = "My_Top_Format"; select($ofh); If you use the English module, you can even read the variable names:
use English; $ofh = select(OUTF); $FORMAT_NAME = "My_Other_Format"; $FORMAT_TOP_NAME = "My_Top_Format"; select($ofh); But you still have those funny calls to select . So just use the FileHandle module. Now you can access these special variables using lowercase method names instead:
use FileHandle; OUTF->format_name("My_Other_Format"); OUTF->format_top_name("My_Top_Format"); Much better!
Since the values line following your picture line may contain arbitrary
expressions (for
format Ident = @<<<<<<<<<<<<<<< commify($n) .
To get a real
format Ident = I have an @ here. "@" . To center a whole line of text, do something like this:
format Ident = @|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| "Some text line" .
The
$format = "format STDOUT = \n" . '^' . '<' x $cols . "\n" . '$entry' . "\n" . "\t^" . "<" x ($cols-8) . "~~\n" . '$entry' . "\n" . ".\n"; print $format if $Debugging; eval $format; die $@ if $@; The most important line there is probably the print . What the print would print out looks something like this:
format STDOUT = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $entry ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~ $entry . Here's a little program that's somewhat like fmt (1):
format = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ~~ $_ . $/ = ""; while (<>) { s/\s*\n\s*/ /g; write; } 2.8.2 Footers
While
Here's one strategy: If you have a fixed-size footer, you can get footers
by checking
Here's another strategy; open a pipe to yourself, using 2.8.3 Accessing Formatting Internals
For low-level access to the formatting mechanism, you may use
formline
and access
$^A
(the
$str = formline <<'END', 1,2,3; @<<< @||| @>>> END print "Wow, I just stored `$^A' in the accumulator!\n";
Or to make an
use Carp; sub swrite { croak "usage: swrite PICTURE ARGS" unless @_; my $format = shift; $^A = ""; formline($format,@_); return $^A; } $string = swrite(<<'END', 1, 2, 3); Check me out @<<< @||| @>>> END print $string; Lexical variables (declared with my ) are not visible within a format unless the format is declared within the scope of the lexical variable. |
|