Recently, I found myself hacking a web application for a customer. If you’ve written a web application or two, you know the type: a multi-page web form where the fields need to be validated, stored into session data, and then finally dispatched into the next phase.
Recently, I found myself hacking a web application for a customer. If you’ve written a web application or two, you know the type: a multi-page web form where the fields need to be validated, stored into session data, and then finally dispatched into the next phase.
Since I’m always carping, “Use the CPAN, please,” I thought I’d find something reusable there for my application. However, after I looked at a number of CPAN modules, I didn’t find what I wanted. CGI::Application looked close, but had more knobs and dials than I needed, and yet not enough custom hooks either.
So, I sat down to write my own. I decided to make a clean cut between the Model, View, and Controller (MVC) parts. My Model code would be Class::DBI-based, because I’d gotten a lot of mileage from it on a prior project. My View code would, of course, be Template-Toolkit driven, because it just works. So, all that was left was the Controller code.
I suspected that most of the Controller would look the same for most of the pages. In pseudo-code, it might look something like this:
if (determine_current_page()) {
if (page_fields_validate() and can_store_page()) {
select_next_page();
load_page_data();
} else {
pick_a_default_page();
load_page_data();
}
}
display_the_page();
On the first coding, I decided that this was more than pseudo-code: it was already Perl. (That happens a lot for me.) But I also knew that the steps to validate a page, store…
Please log in to view this content.
Not Yet a Member?
Register with LinuxMagazine.com and get free access to the entire archive, including: