238 10 Transforming with Traditional Programming Languages
procedure to the parser as a parameter. Without this Perl would simply in-
voke thestartprocedure at this place in the program. In this case we never
explicitly invokestartin our own program. We want the parser to do this
instead. It will be invoked five times for the sample document: once for the
HealthStudyelement and once each for the fourInterviewelements.
The parsing is actually done when theparsefileprocedure is invoked.
This procedure belongs to the parser and is not one of your own procedures
(such as thestartprocedure), so it is invoked by using the->operator on
the module object. Procedures that belong to a module are calledmethods.
The parameter is the name of the file to be parsed. In this case, the name of
the XML file will be specified on the command line. The file names on the
command line are in theARGVarray. Program 10.14 will be run by typing
this line to the computer:
perl printweights.perl healthstudy.xml
Thestartprocedure will be invoked with two kinds of information: the
name of the element and the attributes of the element. The name of the el-
ement (also called its βtagβ) is the second parameter. The first statement of
startsets thetagvariable to the element name for later use. The rest of
the parameters are the attributes of the element. The simplest way to use
these parameters is to convert them to a hash and then look up the attributes
that are needed. The second statement converts the parameters to a hash
namedattributes. The program prints theWeightattribute of every
Interviewelement. The output of the program is
Weight 46.27
Weight 68.95
Weight 92.53
Weight 50.35
The XML::Parser handlers all have a first parameter that is a reference to
an internal parsing procedure. This is used only if one wishes to get access
to low-level parsing information.
One might be curious about what those=>symbols mean in this program.
As it happens, they are just another way of writing a comma. In other words,
one could equally well have constructed the parser using this statement:
$p = new XML::Parser(Handlers, { Start, \&start });
The purpose of the=>symbols is to make the program easier to understand.
It is very common to specify parameters in pairs, where each pair consists of