Ovid has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a script that creates some moderately convoluted HTML. Performance is not an issue, but it may become one depending upon how it's deployed. To write the HTML, I just created a "proof of concept" page and wound up just tossing the straight HTML into the script in <<HERE docs. I'm using CGI.pm for everything else, but I've thought about using the CGI module's methods for creating HTML.

My question: what issues are involved with this? It seems to me that having CGI.pm create the HTML instead of using <<HERE docs is going to be slower. I haven't benchmarked anything yet, but I just wanted to ask other Monks their opinion of this use of this module regarding performance, maintainability, or other issues.

Cheers,
Ovid

Replies are listed 'Best First'.
Re: HTML with CGI.pm
by athomason (Curate) on Jul 22, 2000 at 00:33 UTC
    Since I discovered CGI.pm, I've used it for everything HTML-related that I have to do. I tend to embed tag functions 10-20 deep, which prodocues remarkably concise code. This confuses the hell out of coworkers not used to functional programming, but properly indented, it can make perfect sense. As a result, the code is much more maintainable and infinitely more flexible. Tables especially are vastly simplified by using CGI. Need to change the color of a cell? Do it in one place, not everywhere you have a <TD>. I recently went back to a script I wrote using a combination of hard HTML with Perl buried inside; I had never updated the script because I hadn't been able to decipher what it did anymore. I finally decided to reimplement it from scratch using CGI, and now it's a breeze to change.

    As for efficiency, I've never noticed any slowdown, even with complex table-embedded forms and such. However, I don't manage any sites with huge traffic so I've never bothered to do any benchmarks. My intuition would be that the slowdown would be certainly detectable if you tried to notice it, but probably not fatal.

    If speed is a completely critical issue, try it both ways and do the benchmarks; you may need to do the hard HTML. But if it's not, absolutely get comfortable with CGI.pm; you'll love yourself for it down the road.

RE: HTML with CGI.pm
by Anonymous Monk on Jul 22, 2000 at 19:36 UTC
    CGI.pm can make some aspects producing HTML easier. I would suggest that you set $XHTML = 1; so that you get xhtml compliant html out. I would also suggest using CGI::Pretty for more readable markup. I have started to use HTML::Template to help abstract the program from the presentation. This way you can make one template and reuse it and someone else can do your page creation. Also using HTML::Template you can avoid CGI.pm for most of your html markup and you can control exactly how you format and indent the code.
RE: HTML with CGI.pm
by jlistf (Monk) on Jul 22, 2000 at 00:48 UTC
    in my experience (limited though it is) using CGI.pm is harder to use and the resulting HTML is harder to read. let me explain this. i don't like having to embed some tags three or four or more levels deep. just my preference, i know. as for the reading... make sure you add in newlines yourself in the outputted HTML or you'll have lines of HTML that stretch on endlessly.

    CGI.pm does have some benefits when outputting HTML. it will automatically close all tags, making it unnecessary to worry about that.

    in general, i rarely use it to output HTML (except the header, start_html and end_html methods) and stick to its cookie and parameter methods. i suppose if you got used to coding it and were able to turn the outputted HTML into a easily readable format it would be worthwhile. but i don't do enough CGI programming to do that.

    more: i've never used CGI::pretty so i can't comment on that. as for the speed factor... my guess is that CGI.pm would be a little slower, but i'm not certain.
RE: HTML with CGI.pm
by ar0n (Priest) on Jul 22, 2000 at 00:25 UTC
    All I know (well, not all) is that CGI.pm produces ghastly HTML code. I don't use it because it's just a pain in the ass to look at when some html-code is wrong and you have to look at the source. But I have heard it makes it really easy to produce tables on-the-fly.

    my $0.02

    -- aron

      Replace use CGI; with use CGI::Pretty; and you'll get HTML much more readable and compatible than you probably could code yourself, indented and all. You can also customize which tags it linebreaks on to prevent bad (read: all) browsers from incorrectly interpreting whitespace, which mainly happens in tables.
      It's my understanding that CGI::Pretty gets around this problem, but I haven't had the opportunity to use it since I have pretty much avoided the CGI modules HTML capabilities.
        $, = "\n";
        Would help emensly!
        --
        Casey
        
RE: HTML with CGI.pm
by royalanjr (Chaplain) on Jul 22, 2000 at 18:57 UTC
    I will have to agree about CGI.pm making horrid HTML. What little I have done with it at the moment, I have used it to pass params and so forth, but writing my own HTML. Sounds like I'll have to try out the Pretty...

    Roy Alan