probably one of the easiest to understand reasons for using an OO interface over the function oriented one is to prevent namespace pollution.

if you're using the function oriented interface, you have to be careful that nowhere in your code do you have a subroutine named 'param', or 'upload', or 'header' or any of the other functions that CGI defines. so first of all, you have to be familiar enough with the CGI module to know the full list of functions it exports, then you have to be careful to avoid them in your own code, then you have to be careful that any other modules you use don't export functions with those names. the last part is the real kicker, especially since it makes it more difficult to add modules in the future.

on the other hand, if you're using the OO interface, you can just do

my $cgi = new CGI(); my $foo = $cgi->param('foo'); print $cgi->header(); &param($foo); &header($foo); ... sub param { ... } sub header { ... }

and know that your param and header subs will never get confused with CGI's.

personally, i find that using the OO interfaces to modules looks cleaner and easier to read, as long as things are named well. you always have the '$cgi->' there to give you a hint that the function being called is something to do with CGI.

anders pearson


In reply to Re: OO CGI vs. function-oriented by thraxil
in thread OO CGI vs. function-oriented by krujos

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.