Links: Next Previous Up Top

How should I handle forward references?

Because sgmlspl processes the document as a linear data stream, from beginning to end, it is easy to refer back to information, but relatively difficult to refer forward, since you do not know what will be coming later in the parse. Here are a few suggestions.

First, you could use push_output and pop_output to save up output in a large string. When you have found the information which you need, you can make any necessary modifications to the string and print it then. This will work for relatively small chunks of a document, but you would not want to try it for anything larger.

Next, you could use the ext method to add extra pointers, and build a parse tree of the whole document before processing any of it. This method will work well for small documents, but large documents will place some serious stress on your system's memory and/or swapping.

A more sophisticated solution, however, involves the Refs.pm module, included in this distribution. In your sgmlspl script, include the line

use SGMLS::Refs.pm;

to activate the library. The library will create a database file to keep track of references between passes, and to tell you if any references have changed. For example, you might want to try something like this:

sgml('start', sub {
  my $Refs = new SGMLS::Refs('references.refs');
});
sgml('end', sub {
  $Refs->warn;
  destroy $Refs;
});

This code will create an object, $Refs, linked to a file of references called references.refs. The SGMLS::Refs class understands the methods listed in table 4

Table 4: The SGMLS::Refs class


Method
new(filename,[logfile_handle])
Return Type
SGMLS::Refs
Description
Create a new SGMLS::Refs object. Arguments are the name of the hashfile and (optionally) a writable filehandle for logging changes.

Method
get(key)
Return Type
string
Description
Look up a reference key in the hash file and return its value.

Method
put(key,value)
Return Type
[none]
Description
Set a new value for the key in the hashfile.

Method
count
Return Type
number
Description
Return the number of references whose values have changed (thus far).

Method
warn
Return Type
1 or 0
Description
Print a warning mentioning the number of references which have changed, and return 1 if a warning was printed.

Links: Next Previous Up Top

David Megginson <dmeggins@aix1.uottawa.ca>