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