Files Formats
Formats
Perl has a fairly sophisticated mechanism for generating formatted output. This involves using pictorial representations of the output, called templates, and the function write.
Using a format consists of three operations:
1. Defining the format (template)
2. Loading data to be printed into the variable portions of the format
3. Invoking the format.
Format templates have the following general form:
format FORMATNAME =
fieldline
$value_one, $value_two, …
.
Everything between the “=” and the “.” is considered part of the format and everything (in the fieldlines) will be printed exactly as it appears (whitespace counts). Fieldlines permit variable interpolation via fieldholders:
Hi, my name is @<<<<<, and I’m @< years old
$name, $age
Fieldholders generally begin with a @ and consist of characters indicated alignment/type.
The name of the format corresponds to the name of a filehandle. If write is invoked on a filehandle, then the corresponding format is used. Naturally then, if you’re printing to standard output, then your format name should be STDOUT. If you want to use a format with a name other than that of your desired filehandle, set the $~ variable to the format
name.
There are special formats which are printed at the top of the page. If the active format name is FNAME, then the “top” format name is FNAME_TOP. The special variable $% keeps a count of how many times the “top” format has been called and can be used to number pages.