Skip to Main Content
Perl in a Nutshell
book

Perl in a Nutshell

by Nathan Patwardhan, Ellen Siever, Stephen Spainhour
December 1998
Beginner to intermediate content levelBeginner to intermediate
674 pages
40h 41m
English
O'Reilly Media, Inc.
Content preview from Perl in a Nutshell

Formats

Formats are a mechanism for generating formatted reports for outputting data. Formats are defined with the format keyword. The general form looks like:

format name =
 ...template lines...
                  ...argument line...
.

Most of your format names will be the same as the filehandle names for which they are used. The default format for a filehandle is the one with the same name.

The format definition is like a subroutine definition. It doesn’t contain immediately executed code and can therefore be placed anywhere in the file with the rest of the program; they are commonly placed near the end of the file with subroutine definitions. To output to a format, use the write function instead of print.

The template lines contain literal text and fieldholders. Fieldholders contain symbols that describe the size and positioning of the area on the line where data is output. An argument line immediately follows a template line that contains the fields to be replaced by data. The argument line is a list of variables (or expressions), separated by commas, which fill the fields in the previous line in the order they are listed.

Here’s an example of a template line with two fieldholders, and the argument line that follows:

Hello, my name is @<<<<<<<<<< and I'm @<< years old.
$name, $age

The fieldholders are the @<<<<<<<<<< and @<<, which specify left-justified text fields with 11 and 3 characters, respectively.

Most fieldholders start with @. The characters following the @ indicate the type of field, ...

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.
Start your free trial

You might also like

Perl One-Liners

Perl One-Liners

Peteris Krumins
Beginning Perl

Beginning Perl

Curtis Ovid Poe
Mastering Perl

Mastering Perl

brian d foy
Perl Best Practices

Perl Best Practices

Damian Conway

Publisher Resources

ISBN: 1565922867Catalog PageErrata