<P>Perl has a mechanism to help you generate simple reports and charts. To
facilitate this, Perl helps you code up your output page close to how it
will look when it's printed. It can keep track of things like how many
lines are on a page, what page you're on, when to print page headers,
etc. Keywords are borrowed from FORTRAN: <A HREF="../../lib/Pod/perlfunc.html#item_format"><CODE>format()</CODE></A> to declare and <A HREF="../../lib/Pod/perlfunc.html#item_write"><CODE>write()</CODE></A>
to execute; see their entries in <A HREF="../../lib/Pod/perlfunc.html">the perlfunc manpage</A>. Fortunately, the layout is
much more legible, more like BASIC's PRINT USING statement. Think of it
as a poor man's nroff(1).</P>
<P>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 though.) They have their own namespace
apart from all the other ``types'' in Perl. This means that if you have a
function named ``Foo'', it is not the same thing as having a format named
``Foo''. However, the default name for the format associated with a given
filehandle is the same as the name of the filehandle. Thus, the default
format for STDOUT is named ``STDOUT'', and the default format for filehandle
TEMP is named ``TEMP''. They just look the same. They aren't.</P>
<P>Output record formats are declared as follows:</P>
<PRE>
format NAME =
FORMLIST
.</PRE>
<P>If name is omitted, format ``STDOUT'' is defined. FORMLIST consists of
a sequence of lines, each of which may be one of three types:</P>
<OL>
<LI>
A comment, indicated by putting a '#' in the first column.
<P></P>
<LI>
A ``picture'' line giving the format for one output line.
<P></P>
<LI>
An argument line supplying values to plug into the previous picture line.
<P></P></OL>
<P>Picture lines are printed exactly as they look, except for certain fields
that substitute values into the line. Each field in a picture line starts
with either ``@'' (at) or ``^'' (caret). These lines do not undergo any kind
of variable interpolation. The at field (not to be confused with the array
marker @) is the normal kind of field; the other kind, caret fields, are used
to do rudimentary multi-line text block filling. The length of the field
is supplied by padding out the field with multiple ``<'', ``>'', or ``|''
characters to specify, respectively, left justification, right
justification, or centering. If the variable would exceed the width
specified, it is truncated.</P>
<P>As an alternate form of right justification, you may also use ``#''
characters (with an optional ``.'') to specify a numeric field. This way
you can line up the decimal points. If any value supplied for these
fields contains a newline, only the text up to the newline is printed.
Finally, the special field ``@*'' can be used for printing multi-line,
nontruncated values; it should appear by itself on a line.</P>
<P>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. If an expression evaluates to a number with a
decimal part, and if the corresponding picture specifies that the decimal
part should appear in the output (that is, any picture except multiple ``#''
characters <STRONG>without</STRONG> an embedded ``.''), the character used for the decimal
point is <STRONG>always</STRONG> determined by the current LC_NUMERIC locale. This
means that, if, for example, the run-time environment happens to specify a
German locale, ``,'' will be used instead of the default ``.''. See
<A HREF="../../lib/Pod/perllocale.html">the perllocale manpage</A> and <A HREF="#warnings">WARNINGS</A> for more information.</P>
<P>Picture fields that begin with ^ rather than @ are treated specially.
With a # field, the field is blanked out if the value is undefined. For
other field types, the caret enables a kind of fill mode. Instead of an
arbitrary expression, the value supplied must be a scalar variable name
that contains a text string. Perl puts as much text as it can into the
field, and then chops off the front of the string so that the next time
the variable is referenced, more of the text can be printed. (Yes, this
means that the variable itself is altered during execution of the <A HREF="../../lib/Pod/perlfunc.html#item_write"><CODE>write()</CODE></A>
call, and is not returned.) Normally you would use a sequence of fields
in a vertical stack to print out a block of text. You might wish to end
the final field with the text ``...'', which will appear in the output if
the text was too long to appear in its entirety. You can change which
characters are legal to break on by changing the variable <CODE>$:</CODE> (that's
$FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a
list of the desired characters.</P>
<P>Using caret fields can produce variable length records. If the text
to be formatted is short, you can suppress blank lines by putting a
``~'' (tilde) character anywhere in the line. The tilde will be translated
to a space upon output. If you put a second tilde contiguous to the
first, the line will be repeated until all the fields on the line are
exhausted. (If you use a field of the at variety, the expression you
supply had better not give the same value every time forever!)</P>
<P>Top-of-form processing is by default handled by a format with the
same name as the current filehandle with ``_TOP'' concatenated to it.
It's triggered at the top of each page. See <A HREF="../../lib/Pod/perlfunc.html#write">write in the perlfunc manpage</A>.</P>
<P>While $FORMAT_TOP_NAME contains the name of the current header format,
there is no corresponding mechanism to automatically do the same thing
for a footer. Not knowing how big a format is going to be until you
evaluate it is one of the major problems. It's on the TODO list.</P>
<P>Here's one strategy: If you have a fixed-size footer, you can get footers
by checking $FORMAT_LINES_LEFT before each <A HREF="../../lib/Pod/perlfunc.html#item_write"><CODE>write()</CODE></A> and print the footer
yourself if necessary.</P>
<P>Here's another strategy: Open a pipe to yourself, using <A HREF="../../lib/Pod/perlfunc.html#item_open"><CODE>open(MYSELF, "|-")</CODE></A>
(see <A HREF="../../lib/Pod/perlfunc.html#open()">open() in the perlfunc manpage</A>) and always <A HREF="../../lib/Pod/perlfunc.html#item_write"><CODE>write()</CODE></A> to MYSELF instead of STDOUT.
Have your child process massage its STDIN to rearrange headers and footers
however you like. Not very convenient, but doable.</P>
<P>For low-level access to the formatting mechanism. you may use <A HREF="../../lib/Pod/perlfunc.html#item_formline"><CODE>formline()</CODE></A>
and access <CODE>$^A</CODE> (the $ACCUMULATOR variable) directly.</P>
<P>For example:</P>
<PRE>
$str = formline <<'END', 1,2,3;
@<<< @||| @>>>
END</PRE>
<PRE>
print "Wow, I just stored `$^A' in the accumulator!\n";</PRE>
<P>Or to make an <CODE>swrite()</CODE> subroutine, which is to <A HREF="../../lib/Pod/perlfunc.html#item_write"><CODE>write()</CODE></A> what <A HREF="../../lib/Pod/perlfunc.html#item_sprintf"><CODE>sprintf()</CODE></A>
is to printf(), do this:</P>
<PRE>
use Carp;
sub swrite {
croak "usage: swrite PICTURE ARGS" unless @_;
my $format = shift;
$^A = "";
formline($format,@_);
return $^A;
}</PRE>
<PRE>
$string = swrite(<<'END', 1, 2, 3);
Check me out
@<<< @||| @>>>
END
print $string;</PRE>
<P>
<HR>
<H1><A NAME="warnings">WARNINGS</A></H1>
<P>The lone dot that ends a format can also prematurely end a mail
message passing through a misconfigured Internet mailer (and based on
experience, such misconfiguration is the rule, not the exception). So
when sending format code through mail, you should indent it so that
the format-ending dot is not on the left margin; this will prevent
SMTP cutoff.</P>
<P>Lexical variables (declared with ``my'') are not visible within a
format unless the format is declared within the scope of the lexical
variable. (They weren't visible at all before version 5.001.)</P>
<P>Formats are the only part of Perl that unconditionally use information
from a program's locale; if a program's environment specifies an
LC_NUMERIC locale, it is always used to specify the decimal point
character in formatted output. Perl ignores all other aspects of locale
handling unless the <CODE>use locale</CODE> pragma is in effect. Formatted output
cannot be controlled by <CODE>use locale</CODE> because the pragma is tied to the
block structure of the program, and, for historical reasons, formats
exist outside that block structure. See <A HREF="../../lib/Pod/perllocale.html">the perllocale manpage</A> for further
discussion of locale handling.</P>
<P>Inside of an expression, the whitespace characters \n, \t and \f are
considered to be equivalent to a single space. Thus, you could think
of this filter being applied to each value in the format:</P>
<PRE>
$value =~ tr/\n\t\f/ /;</PRE>
<P>The remaining whitespace character, \r, forces the printing of a new