| 1 | <HTML> |
| 2 | <HEAD> |
| 3 | <TITLE>How should I handle forward references?</TITLE> |
| 4 | </HEAD> |
| 5 | <BODY> |
| 6 | |
| 7 | <P><B>Links</B>: <A HREF=bugs.html>Next</A> <A HREF=skel.html>Previous</A> <A HREF=sgmlspl.html>Up</A> <A HREF=sgmlspl.html>Top</A></P> |
| 8 | |
| 9 | <H1>How should I handle forward references?</H1> |
| 10 | |
| 11 | <P>Because <A HREF=sgmlspl.html><TT>sgmlspl</TT></A> processes the document as a linear data |
| 12 | stream, from beginning to end, it is easy to refer |
| 13 | <EM>back</EM> to information, but relatively difficult to |
| 14 | refer <EM>forward</EM>, since you do not know what will be |
| 15 | coming later in the parse. Here are a few suggestions.</P> |
| 16 | |
| 17 | <P>First, you could use <A HREF=pushoutput.html><TT>push_output</TT></A> and |
| 18 | <A HREF=popoutput.html><TT>pop_output</TT></A> to |
| 19 | save up output in a large string. When you have found the information |
| 20 | which you need, you can make any necessary modifications to the string |
| 21 | and print it then. This will work for relatively small chunks of a |
| 22 | document, but you would not want to try it for anything larger.</P> |
| 23 | |
| 24 | <P>Next, you could use the <A HREF="../SGMLSpm/extend.html"><TT>ext</TT></A> method to |
| 25 | add extra pointers, and build a parse tree of the whole document |
| 26 | before processing any of it. This method will work well for small |
| 27 | documents, but large documents will place some serious stress on your |
| 28 | system's memory and/or swapping.</P> |
| 29 | |
| 30 | <P>A more sophisticated solution, however, involves the |
| 31 | <TT>Refs.pm</TT> module, included in this |
| 32 | distribution. In your <A HREF=sgmlspl.html><TT>sgmlspl</TT></A> script, include the line</P> |
| 33 | |
| 34 | <P> |
| 35 | <PRE>use SGMLS::Refs.pm;</PRE> |
| 36 | </P> |
| 37 | <P>to activate the library. The library will create a database |
| 38 | file to keep track of references between passes, and to tell you if |
| 39 | any references have changed. For example, you might want to try |
| 40 | something like this:</P> |
| 41 | |
| 42 | <P> |
| 43 | <PRE>sgml('start', sub { |
| 44 | my $Refs = new SGMLS::Refs('references.refs'); |
| 45 | }); |
| 46 | sgml('end', sub { |
| 47 | $Refs->warn; |
| 48 | destroy $Refs; |
| 49 | });</PRE> |
| 50 | </P> |
| 51 | <P>This code will create an object, $Refs, linked to a file of |
| 52 | references called <TT>references.refs</TT>. The |
| 53 | <TT>SGMLS::Refs</TT> class understands the methods |
| 54 | listed in table 4</P> |
| 55 | |
| 56 | |
| 57 | <H3>Table 4: The SGMLS::Refs class</H3> |
| 58 | |
| 59 | <HR> |
| 60 | <DL> |
| 61 | <DT><B>Method</B></DT> |
| 62 | <DD><TT>new</TT>(<IT>filename</IT>,[<IT>logfile_handle</IT>])</DD> |
| 63 | <DT><B>Return Type</B></DT> |
| 64 | <DD><TT>SGMLS::Refs</TT></DD> |
| 65 | <DT><B>Description</B></DT> |
| 66 | <DD>Create a new <TT>SGMLS::Refs</TT> object. |
| 67 | Arguments are the name of the hashfile and (optionally) a writable |
| 68 | filehandle for logging changes.</DD> |
| 69 | |
| 70 | </DL> |
| 71 | <HR> |
| 72 | |
| 73 | |
| 74 | <DL> |
| 75 | <DT><B>Method</B></DT> |
| 76 | <DD><TT>get</TT>(<IT>key</IT>)</DD> |
| 77 | <DT><B>Return Type</B></DT> |
| 78 | <DD>string</DD> |
| 79 | <DT><B>Description</B></DT> |
| 80 | <DD>Look up a reference key in the hash file and return its value.</DD> |
| 81 | |
| 82 | </DL> |
| 83 | <HR> |
| 84 | |
| 85 | |
| 86 | <DL> |
| 87 | <DT><B>Method</B></DT> |
| 88 | <DD><TT>put</TT>(<IT>key</IT>,<IT>value</IT>)</DD> |
| 89 | <DT><B>Return Type</B></DT> |
| 90 | <DD>[none]</DD> |
| 91 | <DT><B>Description</B></DT> |
| 92 | <DD>Set a new value for the key in the hashfile.</DD> |
| 93 | |
| 94 | </DL> |
| 95 | <HR> |
| 96 | |
| 97 | |
| 98 | <DL> |
| 99 | <DT><B>Method</B></DT> |
| 100 | <DD><TT>count</TT></DD> |
| 101 | <DT><B>Return Type</B></DT> |
| 102 | <DD>number</DD> |
| 103 | <DT><B>Description</B></DT> |
| 104 | <DD>Return the number of references whose values have changed (thus |
| 105 | far).</DD> |
| 106 | |
| 107 | </DL> |
| 108 | <HR> |
| 109 | |
| 110 | |
| 111 | <DL> |
| 112 | <DT><B>Method</B></DT> |
| 113 | <DD><TT>warn</TT></DD> |
| 114 | <DT><B>Return Type</B></DT> |
| 115 | <DD>1 or 0</DD> |
| 116 | <DT><B>Description</B></DT> |
| 117 | <DD>Print a warning mentioning the number of references which have |
| 118 | changed, and return 1 if a warning was printed.</DD> |
| 119 | |
| 120 | </DL> |
| 121 | <HR> |
| 122 | |
| 123 | |
| 124 | |
| 125 | <P><B>Links</B>: <A HREF=bugs.html>Next</A> <A HREF=skel.html>Previous</A> <A HREF=sgmlspl.html>Up</A> <A HREF=sgmlspl.html>Top</A></P> |
| 126 | |
| 127 | |
| 128 | <ADDRESS>David Megginson <A HREF="mailto:[email protected]"><[email protected]></A></ADDRESS> |
| 129 | </BODY> |
| 130 | </HTML> |