-
Notifications
You must be signed in to change notification settings - Fork 13
by MarkRajcok
For web development, MVC is a term used to describe how to separate a web application into the following three pieces (roughly speaking):
- model = core functionality (domain logic, business rules) and data/content (stored in a database, flat file(s), etc.)
- view = displays information to the user (presentation: HTML, CSS, etc.) based on model data
- controller = handles user input, controls what you see and notifies the model of user actions Both the views and controllers comprise the user interface. The model should not have any knowledge of the views.
CGI::Application helps you obtain the separation:
-
plugin modules such as CGI::Application::Plugin::DBH make it easy to interact with your database/model (but separate "business" objects, not derived from CGI::Application, should be used for the model and hence database interaction)
-
CGI::Application has built-in HTML::Template support, and support for other templating systems, which keeps your view/HTML in separate files (and not mixed with your Perl code)
-
CGI::Application's "run modes" help you divide your application into pieces of controller logic, which normally correspond to pages on a website, or user actions. For example:
package MyOnlineStore; use strict; use base 'CGI::Application'; use CGI::Application::Plugin::AutoRunmode; use CGI::Application::Plugin::DBH (qw/dbh_config dbh/); sub show_items_for_sale : StartRunmode { my $self = shift; my $t = $self->load_tmpl('list_of_items.tmpl'); ... access some business objects to get the items ... ... populate the template with the data ... return $t->output } sub show_single_item : Runmode { ... } sub new_user : Runmode { ... }
For each runmode, you'll often have an HTML template file, which allows you to cleanly separate your view/presentation/HTML from your controller logic/code. This is particularly handy if you're working with people who don't know Perl, but you want/need them to make changes to the HTML (view/presentation). They simply modify the .tmpl files (and CSS files), while you work on the Perl code.
See also
Originally transferred from http://cgi-app.org/index.cgi?MVC