tags:

views:

69

answers:

3

I've been learning about the CGI module lately, and the book I'm using shows there are two ways you can use CGI, function-oriented or object-oriented. They say the benefit of having object-oriented is only to be able to create two CGI objects. First of all is this true, and are there any other benefits, and secondly what example is there for using two CGI objects?

+3  A: 

I think I have found the answer to my question

http://perldoc.perl.org/CGI.html#PROGRAMMING-STYLE

Reading through the faq, an example given for multiple uses of CGI objects is I can store CGI and load previous CGI objects, which is quite useful.

kuroutadori
CGI.pm is very low-level for web programming. There are a lot of [higher-level frameworks](https://www.socialtext.net/perl5/index.cgi?web_frameworks) you can choose from, which handle the fiddly stuff for you.
Philip Potter
Philip Potter has a very good point. That said, I am currently using CGI.pm on a personal project.
HerbN
Well, both interfaces use CGI objects. The functional interface just uses a hidden object.
brian d foy
+1  A: 

Beyond the advantages you cite I'd also point out that OOP usage of CGI.pm is much cleaner to read (at least for me) and manage than the functional version.

I also suspect it is more common so people who have to maintain your code after you (including you six months from now) will find it easier to maintain.

HerbN
+3  A: 

When I need to put together a very simple CGI script, I use the CGI module's OO interface.

I use the OOP interface because the standard, imperative interface imports a ton of symbols that may conflict with my own symbols. I don't like this, so I always prevent symbol importation. I don't use CGI;. Instead, I use CGI ();.

I also limit my use to generating the header and parsing parameters. I always generate HTML as HTML or better yet, use a template module like TemplateToolkit.

I strictly avoid CGI's HTML generation functions. Why?

  • I (along with many other people) already know HTML, and I see no benefit in learning CGI's pseudo-html interface.
  • When a script grows up and needs to be used in another environment, it is easier to extract the HTML blocks or templates and reuse them.

Don't interpret what I've written as a blanket condemnation of CGI.pm. There's plenty to love about CGI.pm. It gets content type generation right. It makes parameter parsing trivial. It is a core module. It makes command line debugging and testing easy.

daotoad