Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Web presentation patterns, MVC, and reinventing the wheel

by relax99 (Monk)
on Jan 30, 2003 at 20:02 UTC ( #231421=perlquestion: print w/ replies, xml ) Need Help??
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!

Comment on Web presentation patterns, MVC, and reinventing the wheel
Download Code
Re: Web presentation patterns, MVC, and reinventing the wheel
by dragonchild (Archbishop) on Jan 30, 2003 at 20:36 UTC
    It looks like you have some sort of event-driven architecture. My first instinct is to use callbacks. Something like:
    my %callbacks = ( foo => \&do_foo, bar => \&do_bar, etc ... ); my $action = $cgi->param('action'); my $callback = $callbacks{$action}; die "No callback!\n" unless $callback; return $callback->(@args);
    Then, you work some neat mechanism of registering the callbacks. They can even be the result of can (but you have to have an appropriate object to call it on) or method names (to be called on appropriate objects, presumably with can).

    Of course, using CGI::Application is good cause it encapsulates the annoying work for you. :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

•Re: Web presentation patterns, MVC, and reinventing the wheel
by merlyn (Sage) on Jan 30, 2003 at 21:10 UTC
Re: Web presentation patterns, MVC, and reinventing the wheel
by perrin (Chancellor) on Jan 30, 2003 at 21:11 UTC
    (I'm the author of the article you're referring to)

    The big secret about MVC is that it's pathetically easy, and much easier in Perl than in Java because of the great templating solutions available and the ease of passing data around. (Java's strong typing often leads the authors of Java templating tools to create their own makeshift version of Perl's basic data types.) You are already doing it (assuming you use templates or something for the HTML generation), and there's really nothing wrong with what you have. As you point out, CGI::Application just saves you from coding the switch logic yourself.

    Personally, I like to split up a site into multiple applications to keep the pieces small. Editing user data might be one, browsing a catalog might be another, searching another, etc. This keeps things more manageable.

    If you want to see how other people have done it, I suggest you look at this page. In particular, OpenInteract is pretty close to what you're doing.

Re: Web presentation patterns, MVC, and reinventing the wheel
by TGI (Vicar) on Jan 31, 2003 at 00:46 UTC

      Ok I'll weigh in here with only one insight, which is to say that RT version 1 is IMHO and experience a very poor scaler and has behaved abominably after much use. RT2 I have messed about with but not deployed .. mayhap they have improved it's scalability... i recall v2 supporting various DB interfaces.... actually - gulp - rt2 is probably worth a look.


      I can't believe it's not psellchecked

        I haven't looked at RT1, but RT2 is pretty impressive. I haven't used it under heavy load, but it ran acceptably on my horrendously underpowered server (200 MHz PPro, 128 MB RAM, hosting PostgreSQL + Apache/mod_perl). BTW, RT3 beta was just released.

        You can try RT2 out at rt.perl.org. You can play with it here (the guest password is guest).


        TGI says moo

Re: Web presentation patterns, MVC, and reinventing the wheel
by tomhukins (Curate) on Jan 31, 2003 at 17:30 UTC
Re: Web presentation patterns, MVC, and reinventing the wheel
by jk2addict (Chaplain) on Jan 31, 2003 at 19:41 UTC

    This is just my opinion, but I never really liked Mason, or TTK because the format of where and how the tags were called still seemed to code-like. I migrated towards AxKit. For me personally, it seems to have the best seperation of the MVC layers, and since it is based on XML, the template tags (taglib tags) were cleaner since they looked just like the XML/HTML used for the rest of the template.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://231421]
Front-paged by Kanji
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (14)
As of 2014-07-24 16:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (162 votes), past polls