Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I guess everyone has written a medium CGI application at least once. I did it several times, each time discovering something new to make my application more scalable.

Now I think that a single script with run-modes provide the best framework to develop CGI.
My code usually looks like this:

my %dispatch_table = ( list => \&list, search_form => \&search_form, help => \&help, # And so on... ); my $mode = $query->param( 'mode' ); print $query->header, $query->start_html(); &{$dispatch_table{$mode}}; print $query->end_html;

Then I write a procedure for every run-mode I need. Using Template module I need also a template file for every type of page that my CGI application will generate (that often corresponds to a single run mode) so that I could add functionality to my application just adding a new term in my %dispatch_table and writing a proper new procedure. I fell very comfortable with this developing process. But I'm sure I didn't say anything new for most part of you.

Unfortunately designing a CGI application often drives to more complicated situations, where a run-mode implies another run-mode and so on, asking the developer to pass parameter through the application, just because a run-mode has to pass them to the run-mode that will actually need them. I'll explain this situation with an example.

Let's suppose we are developing a web-based forum. So there will be a list run-mode that displays every message in the forum, allowing the user to reply to a particular message. Also, there will be a form run-mode that will provide... a form to let the user insert the reply. In this form there will be a submit button that will invoke insert_message run-mode. This mode contains DBI stuff to actually insert the new message in the forum, reporting success or error to the user.

Here a schematization:

list mode -> form mode -> insert_message mode
We are passing info about the message we're replying to. form mode does not need this infos, but we need to pass them to insert_message mode. Eventually we pass informations to insert-message mode, that will use them, e.g., to store the message in the same thread of its parent.

What does it means? form run-mode fetches proper parameter, will pass it to the template that will probably add a hidden field to pass this parameter to insert_message mode. I think this approach is too complicated and makes the application difficult to scale. The linear process of adding new procedures and entries in the %dispatch_table breaks when the functionality provided by the application is formed by several dipendent screens.

What can be done to cope with such situations? I thought to a couple of solutions, but I'm not sure of their effectiveness.

  • Treating complex functionalities in complex procedures, using another dispatch table. In the example I was talking about, we could unify form and insert_message run-modes in a single reply run-mode, that will have its own dispatch table with form and insert_message modes. I don't like this approach too much because it takes off the possibility to use a procedure in multiple situations (e.g.: we could use form and insert_message modes to insert in the forum messages that are not replies.
  • CPAN :) There are CGI::Application (that, as far as I know, does not introduce new functionality to the tecniques I've described above), CGI::Screen and CGI::FormMagick. I haven't tried the last two. Does anyone?

In reply to Scaling single-script CGI applications by larsen

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others perusing the Monastery: (9)
    As of 2019-07-22 12:48 GMT
    Find Nodes?
      Voting Booth?
      If you were the first to set foot on the Moon, what would be your epigram?

      Results (17 votes). Check out past polls.