The CGI::Application (C::A) docs notes that the instance script doesn't do very much and it doesn't. I've come round to the view that it can be better if your run modes don't do much either.

The docs discuss the idea of having a “base” module (say, MyBase.pm) that inherits from C::A and that your cgi app inherits from that. MyBase.pm takes care of common setup e.g. the query, state, config, db and could therefore be used by many/all of your cgi apps. But only cgi specific setup. My experience is that you are tempted to get too clever and add too much and you will regret it.

Your cgi app then only has to worry about run modes. The run modes take any input from the query, session or config and calls the application proper. The app returns a data structure and the run mode saves any session data and drops the appropriate param into an HTML::Template. The application api accepts input, returns output and is blissfully unaware of who or what has called it. The run mode is short and simple, it has no clue as to what the application does.

MyBase.pm is easy to test (and the simpler it is the better), once it is working you can forget about it. A dummy app can test the run modes - checking that the correct input is supplied and can handle results correctly (this where you can tweak your html).

The big gain in my opinion is that a non cgi instance script can now test your application while you develop it without having to worry about anything cgi related (httpd, query, state, html etc.). Nail down the api, once that is hammered out you'll know what run modes you need and what they need to do. If the api includes methods like $app->is_success, $app->session_data, $app->HT_tmpl, $app->HT_param etc. the run mode can easily handle any house keeping chores.

All the serious work, all the heavy lifting is done by the application which is, I believe, the best place to do it. I've found that this “separation of concerns”, where cgi is used purely as a front end to a separate, stand alone application can help enormously with speed of development and maintenance of my cgi apps.

Skinny run modes; not rocket science but can make a big difference.


In reply to Taking your application out of CGI::Application (skinny run modes) by wfsp

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.