On Thursday 04 October 2001 01:43, Jeffrey W. Baker wrote: > 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"; > }
Yes, and I agree with you that this approach has severe drawbacks. Years of practice have made it usable through templating systems and various tricks that people discover as they learn. The linearity is one of the main problems as if the beginning of your output is dependent on something farther down you often have to resort to complex lookaheads. There are also problems with coupling, in that code meant to process one data structure into a string will often be able to process only that data structure. It's hard to make this approach generic, or even to abstract large parts of it. And recursing into arbitrarily deeply nested structures is up to you, whereas using the DOM with XPath (or iterators), or XSLT takes care of that for you. Another major advantage is pipeline processing. You can have several processors in a row, all of them specialised and dealing with the same kind of data structure (a DOM). This is similar to using filters, but filters are painful and require reparsing at every stage. I find it much easier to use a pipeline model, especially when I want to add a new feature as that new feature will live on its own, separate from the rest of the code. > 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). I gave up on XML::DOM the day I tried to patch it to make it DOM 2 compliant (or even only to add namespace support). I tend to use SAX whenever I don't need lookahead, and XML::LibXML for everything DOM related. It isn't 100% complete but it's close, and will soon be. Watch perl-xml for an annoucement of PerlDOM, as it seems likely to happen soon. In all of my recent websites I've used combinations of DOM and SAX processing, always with a pipeline approach. Most of the time this happens in AxKit but there are exceptions where I decided to hand build a few things. I also tend to make a growing use of XSP as it allows you to express the output XML DOM you want from your code in quite a direct manner. As all templating-like approaches you've got to be discplined for it to be readable, but that's easy enough. And more importantly, writing taglibs (in order to avoid embedded code) is rather trivial if you know SAX (and SAX is far from being hard to learn). -- _______________________________________________________________________ Robin Berjon <[EMAIL PROTECTED]> -- CTO k n o w s c a p e : // venture knowledge agency www.knowscape.com ----------------------------------------------------------------------- "My girlfriend always laughs during sex, no matter what she's reading" -- Steve Jobs