Defining a Format

A format is defined using a format definition. This format definition can appear anywhere in your program text, like a subroutine. A format definition looks like this:

format someformatname =
fieldline
value_one, value_two, value_three
fieldline
value_one, value_two
fieldline
value_one, value_two, value_three
.

The first line contains the reserved word format, followed by the format name and then an equal sign (=). The format name is chosen from yet another namespace, and follows the same rule as everything else. Because format names are never used within the body of the program (except within string values), you can safely use names that are identical to reserved words. As you’ll see in the following section, Section 11.3 most of your format names will probably be the same as filehandle names (which then makes them not the same as reserved words...oh well).

Following the first line comes the template itself, spanning zero or more text lines. The end of the template is indicated by a line consisting of a single dot by itself.[74] Templates are sensitive to whitespace; this instance is one of the few in which the kind and amount of whitespace (space, newline, or tab) in the text of a Perl program actually matters.

The template definition contains a series of fieldlines. Each fieldline may contain fixed text—text that will be printed out literally when the format is invoked. Here’s an example of a fieldline with fixed text:

Hello, my name is Fred Flintstone.

Fieldlines ...

Get Learning Perl on Win32 Systems now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.