ЭЛЕКТРОННАЯ БИБЛИОТЕКА КОАПП |
Сборники Художественной, Технической, Справочной, Английской, Нормативной, Исторической, и др. литературы. |
11.3 Invoking a FormatYou 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, which is called Stonehenge:4470 SW Hall Suite 107:Beaverton:OR:97005 Fred Flintstone:3737 Hard Rock Lane:Bedrock:OZ:999bc In other words, five colon-separated fields, which our code parses as described below. The Once 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 | =============================== |