10.2 Transforming XML 249
print("
while (<>) {
chomp;
split(/:/);
print("
}
print("
Program 10.20 Converting text data to XML
Template 10.1 Perl template for converting text data to XML
program because the XML text is spread throughout the Perl code. The Perl
template is shown in template 10.1.
Notice how the Perl template looks much more like the output that is to be
produced than the Perl program. The parts of the template in bracketed per-
cent signs are thevariableparts of the template. The rest of the template is
theconstantpart. The constant part is printed exactly as shown. The variable
parts areinstantiatedwith the values of what look like variables. However,
the namesidandcontentare actually hash keys, not variables. The tem-
plate is used from program 10.21.
The first line of the program tells Perl that the Template Toolkit package is
being used. The data are obtained by reading the first line of the input file
and extracting the data to be used in the template. The last part of the pro-
gram invokes the template package. The first statement constructs the tem-
plate processor using the Template Toolkit package. The second statement
constructs a hash that tells the template processor the data that should be
used for instantiating the template. As noted earlier, what look like variables
in the template are actually hash keys. The third statement actually does the
processing. The template processor needs two parameters: the name of the
template file and the hash containing the data to be used for instantiation of
the template.