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

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

Hello,

Not so long ago I discovered that it is a good idea to use MVC web presentation pattern when programming complex web applications. I was able to get bits and pieces on how to do that from the article on perl.com "Building a Large-scale E-commerce site with Apache and mod_perl" which I'm sure most of you are familiar with. Then, the same pattern is described in "Pattens of Enterprise Application Architecture" by Martin Fowler and numerous articles online. However, the examples there are in Java. I haven't been able to find any good examples of how to structure a web application in Perl. All Perl books contain rather beginner information on CGI and they don't have any information on how to structure big real world web applications in Perl.

Before, the way I structured my code was to separate the model in one or few Perl modules and have the main script use instances of these models. The main script would proceed based on the actions from the user and is generally structured as a huge switch statement like the one below. The web pages are based on HTML templates with dynamic data inserted into them before they're sent to the browser.

my $cgi = CGI::Minimal->new; my $action = $cgi->param("action"); for( $action ) { /list_all_users/ and do { # either a function call or more code here last; }; /add_user/ and do { # either a function call or more code here last; }; /edit_user/ and do { # either a function call or more code here last; }; # and so on } # code to output resulting web page

This approach worked for me, but I wonder if there's something better? I'm mostly concerned with reuse and maintainability issues.

One other approach that I saw was to use CGI::Application, but it is really the same as my switch statement when you look close at it.

Please feel free to share your thoughts on what a good way to structure a complex web application in Perl is. Right now I feel like I'm reinventing the wheel, because I basically have to come up with the same conclusion and MVC-like structure as, I'm sure, many people had already. (In companies that use Perl as one of their main development languages). It's really frustrating how Perl books teach you CGI programming, but almost never go beyond using HTML-generating functions in CGI.pm (bad idea!)

Do you know of any
- good sources of information on web presentation patterns in Perl?
- production quality sample source code used in real world?
- discussion threads related to this?

Thanks a lot in advance!