I believe that the canonical way to output a document using any web
scripting language (Perl CGI, mod_perl, PHP, ASP, etc.) is to simply print
out your markup, like this fictional example:

while (my $row = $sth->fetchrow_arrayred()) {
        print "<tr><td>$row->[0]</td><td>$row->[1]</td></tr>\n";
}

There are two main drawbacks to this style of output.  The first is that
print() can mean a trip down the output stack and back up.  The second is
the unavoidable linearity output.  There is no way to back up and change
something once you have decided to print it.

In my more recent web sites, I have been taking a slightly different
approach by building the document from scratch using the DOM, and printing
the DOM tree out at the end of the program.  I perceive several
advantages:

* I can add or remove content and markup anywhere in the document at any
time.

* I avoid I/O functions until the final batch I/O at the end.

* I never have markup errors because the DOM tree always prints itself out
properly.

I'd love to hear any other experiences with using the DOM to build output
from scratch.  I'd also like to hear people's opinions on XML::DOM (I
think it stinks and I've replaced it with a C DOM implementation).

-jwb

Reply via email to