http://www.perlmonks.org?node_id=549374


in reply to CGI::Application - why?

You might as well ask why bother making subroutines, or why bother using OO, since the answers are essentially the same. To give you a real world example, at my work we have built a base class that automates the common pattern of show the form, check for errors, show the errors or process the input, and show a confirmation page. We can add new forms with all of that functionality by filling in a couple of methods (the validation profile and the processing for successful input). We also got a bunch of this for free by using CGI::Application::Plugin::ValidateRM.

Usually, OO and modular code are not about reducing code size. They are about isolating pieces of the code from each other and gaining reuse. Using a module for your web app will help a lot with that.

Replies are listed 'Best First'.
Re^2: CGI::Application - why?
by gunzip (Pilgrim) on May 15, 2006 at 02:43 UTC
    I wanted to be able to factor my web applications into many moudules, not one which is just as big as the original CGI script. The CGI::Application examples seem to focus on getting everything into one big module so where's the modularity? I'd like to have my runmodes in distinct modules, not simply subs within one monolithic module/script.
      CGI::Application is intended to help you map HTTP requests to method calls. You can use that however you like. A common approach is to make a set of related actions into one application, and then factor out all the common code into separate modules, reducing the actual runmode code as much as possible.

      If you prefer, you can make every single action call a different module, but that would probably be massive overkill, especially since one runmode frequently calls another (e.g. if there are input errors you call the mode to show the form again, passing the errors along).

      So then do that. CGI::Application does not prevent it.
        Nor does if ... elsif ... elsif ... elsif ... else. So I still fail to see the point.
      Well, that's just what many CGI::Application users do.

      Since I started using CGI::Application a little over two years ago I find that I can get massive code re-use. Using things like Class::DBI and the many CGI::Application plug-ins now available I find that I can produce a complete web application in as little as 50 or 60 lines of code. Plus the HTML templates of course. By combining HTML::Template (I know, everybody tells me to use Template::Toolkit, but I am comfortable with HTML::Template), the session management and authenticaion plug-ins, CSS and a collection of common support modules, I have been known to produce complete 10 form applications inside a day.

      Maintenance time is drastically reduced as well. By factoring out all the common code into another set of modules, and having the runmodes as small as possible I find that a 20,000 line CGI::Application based product is vastly easier to maintain than its 20,000 line conventional CGI predecessor which I still maintain for one client.

      jdtoronto

      I found that CGI::Prototype did a good job of raising the abstraction that CGI::Application started towards. When you develop a web app under CGIP, you dispatch to modules not subroutines. And the dispatch mechanism is more general: it can be based on anything that you want: GET versus POST, any particular parameter, etc.
      Did you read the nodes in my reply to your original post? In them, I discuss breaking a large CGI application into related sub-applications.

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?