[Skip to the main content...]
Sitemap | Home

dev dot slaggle

Links:

Categories:

Archives:

Search dev dot slaggle

Authentication with CGI::Application

[ Perl ] Posted by: Tim Gourley on 10/15/2004 @ 2:38 PM

The thing I love about Perl is the mantra of TMTOWTDI: There’s more than one way to do it. With programming web applications using Perl, that mantra definitely holds true.

My favorite method of creating web applications in Perl has to be using the application framework CGI::Application written by Jesse Erlbaum. CGI::Application allows you to create applications by abstracting a lot of the different tasks: your CGI::Application module handles the “flow control” of the application through the concept of run modes. HTML::Template handles the “view” of the application by providing the ability to seperate the HTML into different templates for each display of your application. Finally, the business logic can be encapsulated into seperate classes that CGI::Application calls.

Abstracting out the different parts of your application like this is called the MVC (or Model-View-Controller) architecture, first popularized by the Smalltalk programming language in the 1970’s.

Here is a diagram of what it looks like:

MVC Architecture

With MVC you have three major states: the Model, the View, and the Controller (obviously). In the case of authoring a web application with CGI::Application, the CGI::Application class you write is the Controller, which dispatches to various run modes based on input from the user in the View mode. The Controller should communicate with external business logic classes that make up the Model portion of the architechture. I say “should” because unfortunately sometimes it is easier to write your business code directly in the CGI::Application class, which is rather sloppy even though it is allowed.

Once dispatched to a run mode and the proper business logic is applied, control is turned over to the View, which in this case is HTML::Template. HTML::Template seperates HTML code from the rest of your Perl application by loading template files and parsing special tags with the proper output. The default HTML::Template class has a limited set of logic tags available, which is great because it forces the logic and control to be handled by the other parts of the application while HTML::Template can focus on the display.

Seperating the code from the design is perfect if you are working with web designers who don’t happen to be familiar with Perl. They don’t have to know the language in order to write templates with HTML::Template, and you don’t have to know how to design perfectly beautiful interfaces.

For those of you familiar with Struts, you may be thinking that CGI::Application isn’t quite an MVC architecture because you have the chance to “cheat” more than you do with Java. In Perl you aren’t strictly required to follow the rules of the architecture. In fact you don’t have to follow many rules at all. Variable typing is pretty loose, object-orientation is loose, and so is using the MVC architecture. But good Perl programmers won’t resort to cheating too much. ;)

Now that we have the concepts in our minds, how do we write an application using CGI::Application? On the following pages I will show you how to create the simplest of applications that will authenticate a user, effectively logging him in to your site. This is one of the tasks that come up most often when programming web applications, so why not start with that?

Before we continue, however, let me say that this article is not for those of you who are new to Perl. You should have some familiarity with the language, including object-oriented programming. Being familiar with CGI::Application before reading further will be a tremendous help. You should know how to read perldocs from the command line or from CPAN’s website. The links to the modules used here are included throughout as well as the at the end of the article if you need more information.

Comments »

The URI to TrackBack this entry is: http://dev.slaggle.com/archives/2004/10/15/authentication-with-cgiapplication/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

(required)

(required)