11.3 Invoking a Format
You invoke a format with the
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 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
After we have all of the variables loaded up (so that the values used by the format are correct), the
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 =============================== | Stonehenge | | 4470 SW Hall Suite 107 | | Beaverton , OR 97005 | =============================== =============================== | Fred Flintstone | | 3737 Hard Rock Lane | | Bedrock , OZ 999bc | =============================== |
|