http://qs321.pair.com?node_id=439482

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

I'm considering porting an existing, moderately complex set of cgi scripts to CGI::Application. I'm new to it, however, and its not at all clear what the best structure of is. Its about 15 scripts, maybe 20 run modes. Not what you'd call large at all, really, but there is no way I'm putting it all in one module. (much of the code is already in db and other modules, but I'd still have a monster).

This is obviously a topic thats been covered before, so any links welcome. I've seen the wiki and I've trawled through the mailing list, and, of course, Super Search. But I've probably missed lots.

I see three options, and I'm not crazy about any:

  1. Convert, roughly, each script to a different cgi app, using a common superclass. Each would have their own setup and run modes. However this leaves me with lots of little stub scripts in place of the originals, which differ only in the class they use. Ugly.
  2. One application, one instantiating script. The application is a big run mode dispatcher that finds the methods in several classes, but doesn't implement any in the main package. Hide much of the run mode information in PATH_INFO to make the urls a bit prettier:  admin.pl/foo/bar/baz gets mapped to Foo::Bar::baz. But I do like per-page setup methods, which this loses.
  3. Hybrid of the above. Separate applications, but a single dispatching script (no need to use CGI::Application) that reads the PATH_INFO and invokes the appropriate application.

thanks, qq

Replies are listed 'Best First'.
Re: complex CGI::Application structure
by mpeters (Chaplain) on Mar 14, 2005 at 22:58 UTC
    You might take a look at CGI::Application::Dispatch which does exactly what you are asking for.

    I you are running under mod_perl, you can set CGI::Application::Dispatch to be your response handler and it will delegate to the appropriate classes with out any instance scripts.

    If you are running under vanilla cgi (mod_cgi) you can use just one instance script using the dispatch() method to replace all of the other little scripts.

    As you pointed out, you do lose the individual new() calls to each module, but you still have a setup() method in each module.

      that looks very nice, thanks.