Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

comment on

( [id://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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 07:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found