Learning Perl on Win32 Systems

Learning Perl on Win32 SystemsSearch this book
Previous: 11.2 Defining a FormatChapter 11
Formats
Next: 11.4 More About the Fieldholders
 

11.3 Invoking a Format

You invoke a format with the write function. This operator takes the name of a filehandle and generates text for that filehandle using the current format for that filehandle. By default, the current format for a filehandle is a format with the same name (so for the STDOUT filehandle, the STDOUT format is used), but we'll soon see that you can change it.

Let's take another look at that address label format, and create a file full of address labels. Here's a program segment:

format ADDRESSLABEL =
===============================
| @<<<<<<<<<<<<<<<<<<<<<<<<<< |
$name
| @<<<<<<<<<<<<<<<<<<<<<<<<<< |
$address
| @<<<<<<<<<<<<<<<<, @< @<<<< |
$city,          $state, $zip
===============================
.

open(ADDRESSLABEL,">labels-to-print") || die "can't create";
open(ADDRESSES,"addresses") || die "cannot open addresses";
while (<ADDRESSES>) {
        chomp; # remove newline
        ($name,$address,$city,$state,$zip) = split(/:/);
            # load up the global variables
        write (ADDRESSLABEL); # send the output
}

Here we see our previous format definition, but now we also have some executable code. First, we open a filehandle onto an output file called labels-to-print. Note that the filehandle name (ADDRESSLABEL) is the same as the name of the format. This fact is important. Next, we open a filehandle on an address list. The format of the address list is presumed to be something like:

Stonehenge:4470 SW Hall Suite 107:Beaverton:OR:97005
Fred Flintstone:3737 Hard Rock Lane:Bedrock:OZ:999bc

In other words, the format is five colon-separated fields, which our code parses as described below.

The while loop in the program reads each line of the address file, gets rid of the newline, and then splits the remainder into five variables. Note that the variable names are the same names as the ones we used when we defined the format. This fact, too, is important.

After we have all of the variables loaded up (so that the values used by the format are correct), the write function invokes the format. Note that the parameter to write is the filehandle to be written to, and by default, the format of the same name is also used.

Each field in the format is replaced with the corresponding value from the next line of the format. After the two sample records given above are processed, the file labels-to-print contains:

===============================
| Stonehenge                  |
| 4470 SW Hall Suite 107      |
| Beaverton        , OR 97005 |
===============================
===============================
| Fred Flintstone             |
| 3737 Hard Rock Lane         |
| Bedrock          , OZ 999bc |
===============================


Previous: 11.2 Defining a FormatLearning Perl on Win32 SystemsNext: 11.4 More About the Fieldholders
11.2 Defining a FormatBook Index11.4 More About the Fieldholders