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, ...
Get Learning Perl on Win32 Systems now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.