Well, I think this is actually two questions -- why use CGI to dynamically generate HTML, rather than hard coding your HTML, and second, why use the OO interface to CGI, rather than the function calls.

The OO implementation ismay be faster than the function calls. So, that answers #2. (but you're not using the OO syntax, so let's get to the other part).

Now for the question, why use functions to create everything? I don't know for everyone, but I think part of it is a historical thing -- there was a time when HTML was in flux, and new versions were coming out. The big problem, with the change from HTML 3.2 to HTML 4.0 was that a bunch of tags were deprecated. (which is why there's 4.0 transitional, and 4.0 strict) If you have the extra layer of encapsulation, you can do checking, to make sure that the values are valid, and that you're not using tags that you're not supposed to.

You also have the advantage that Perl will generate syntax errors if you don't have matching parenthesis, rather than needing to check the HTML afterwards. (well, it's still a good idea to validate your HTML).

There is also the advantage that you can remove highly repetitive tasks. Some of the potentially most annoying bits in generating HTML are the putting in selected/checked attributes on form inputs. (loop through, find right item(s), and add the extra code).

So yes, there are times when there are advantages to not hard coding your HTML... but well, I tend to go the hard coded route. It's the way I learned to do things, I prefer having the extra control of exactly what gets sent. I keep meaning to learn some of the various templating modules, but I've never had the downtime to mess with them. (and it'd require the other 3 programmers on the project to learn it as well)

Some people have problems with update inconsistencies when maintaining hard coded HTML (making sure that all of the items in a list are similarly modified), but CSS makes things easier, as do text editors with good find/replace functions.

As for the 'free' scripts, well, some of those have been around since the days of Perl 4... they're not exactly things to strive for. (A few 'popular' ones in particular come to mind... *cough* formmail *cough*) Many times, they're written by someone who's just starting to code, and may not know the best practices, especially because they're trying to make it generic enough for anyone to use.

update: 'is faster' didn't go with the rest of the comments about benchmarking... see below


In reply to Re: why use OO nature in CGI? by jhourcle
in thread why use OO nature in CGI? by sulfericacid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.