| 1 |
<HTML> |
| 2 |
<HEAD> |
| 3 |
<TITLE>What about the handler argument?</TITLE> |
| 4 |
</HEAD> |
| 5 |
<BODY> |
| 6 |
|
| 7 |
<P><B>Links</B>: <A HREF=generic.html>Next</A> <A HREF=specs.html>Previous</A> <A HREF=sgmlspl.html>Up</A> <A HREF=sgmlspl.html>Top</A></P> |
| 8 |
|
| 9 |
<H1>What about the <IT>handler</IT> argument?</H1> |
| 10 |
|
| 11 |
<P>The second argument to the <TT>sgml</TT> subroutine is |
| 12 |
the actual code or data associated with each event. If it is a |
| 13 |
string, it will be printed literally using the |
| 14 |
<TT>output</TT> subroutine from the <A HREF=output.html><TT>SGMLS::Output</TT></A> library; if |
| 15 |
it is a reference to a <A HREF="http://www.metronet.com/0/perlinfo/perl5/manual/perl.html"><TT>perl5</TT></A> subroutine, the subroutine will be |
| 16 |
called whenever the event occurs. The following three |
| 17 |
<TT>sgml</TT> commands will have identical results:</P> |
| 18 |
|
| 19 |
<P> |
| 20 |
<PRE># Example 1 |
| 21 |
sgml('<DOC>', "\\begin{document}\n"); |
| 22 |
|
| 23 |
# Example 2 |
| 24 |
sgml('<DOC>', sub { |
| 25 |
output "\\begin{document}\n"; |
| 26 |
}); |
| 27 |
|
| 28 |
# Example 3 |
| 29 |
sub do_begin_document { output "\\begin{document}\n"; } |
| 30 |
sgml('<DOC>', \&do_begin_document);</PRE> |
| 31 |
</P> |
| 32 |
<P>For simply printing a string, of course, it does not make sense |
| 33 |
to use a subroutine; however, the subroutines can be useful when you |
| 34 |
need to check the value of an attribute, perform different actions in |
| 35 |
different contexts, or perform other types of relatively more |
| 36 |
complicated post-processing.</P> |
| 37 |
|
| 38 |
<P>If your handler is a subroutine, then it will receive two |
| 39 |
arguments: the <A HREF="../SGMLSpm/sgmlspm.html"><TT>SGMLS.pm</TT></A> event's data, and the <A HREF="../SGMLSpm/sgmlspm.html"><TT>SGMLS.pm</TT></A> event |
| 40 |
itself (see the <A HREF="../SGMLSpm/sgmlspm.html"><TT>SGMLS.pm</TT></A> <A HREF="../SGMLSpm/events.html">documentation</A> for a description |
| 41 |
of event and data types). The following example will print |
| 42 |
<TT>'\begin{enumerate}'</TT> if the value of the attribute |
| 43 |
<TT>TYPE</TT> is <TT>'ORDERED'</TT>, and |
| 44 |
<TT>'\begin{itemize}'</TT> if the value of the attribute |
| 45 |
<TT>TYPE</TT> is <TT>'UNORDERED'</TT>:</P> |
| 46 |
|
| 47 |
<P> |
| 48 |
<PRE>sgml('<LIST>', sub { |
| 49 |
my ($element,$event) = @_; |
| 50 |
my $type = $element->attribute('TYPE')->value; |
| 51 |
|
| 52 |
if ($type eq 'ORDERED') { |
| 53 |
output "\\begin{enumerate}\n"; |
| 54 |
} elsif ($type eq 'UNORDERED') { |
| 55 |
output "\\begin{itemize}\n"; |
| 56 |
} else { |
| 57 |
die "Bad TYPE '$type' for element LIST at line " . |
| 58 |
$event->line . " in " . $event->file . "\n"; |
| 59 |
} |
| 60 |
});</PRE> |
| 61 |
</P> |
| 62 |
<P>You will not always need to use the <IT>event</IT> |
| 63 |
argument, but it can be useful if you want to report line numbers or |
| 64 |
file names for errors (presuming that you called <TT>sgmls</TT> or <A HREF="http://www.jclark.com/sp.html"><TT>nsgmls</TT></A> |
| 65 |
with the <IT>-l</IT> option). If you have a new version |
| 66 |
of <A HREF="http://www.jclark.com/sp.html"><TT>nsgmls</TT></A> which accepts the <IT>-h</IT> option, you |
| 67 |
can also use the <IT>event</IT> argument to look up |
| 68 |
arbitrary entities declared by the program. See the <A HREF="../SGMLSpm/sgmlsevent.html">SGMLS_Event</A> documentation for |
| 69 |
more information.</P> |
| 70 |
|
| 71 |
|
| 72 |
<P><B>Links</B>: <A HREF=generic.html>Next</A> <A HREF=specs.html>Previous</A> <A HREF=sgmlspl.html>Up</A> <A HREF=sgmlspl.html>Top</A></P> |
| 73 |
|
| 74 |
|
| 75 |
<ADDRESS>David Megginson <A HREF="mailto:[email protected]"><[email protected]></A></ADDRESS> |
| 76 |
</BODY> |
| 77 |
</HTML> |