///Using Perl to Create Reusable Web Applications

Using Perl to Create Reusable Web Applications

Object Technologies and HTML Templates in CGI Programming

Perl is a convenient and effective tool for complex Web applications development. However, even experienced programmers resist Perl because it seems difficult to learn and use. This article demonstrates that object-oriented implementation of Perl simplifies the effort and could be much more effective than other Web technologies, especially with separate design and application functionality.

Object-oriented implementation in CGI-scripting is unpopular, as I discovered while trying to find a good guestbook script. I wanted a script that I could easily modify by changing the design, adding new features, etc., and that I could use to build a forum. Of the thirty free guestbook scripts on the Web, none was suitable for me. So I turned to object technology as a solution for reusable Web applications based on HTML templates.

Object and Class Relationships

Before designing the object model it is a good idea to examine existing CPAN (Comprehensive Perl Archive Network) modules that could be useful. The principal issue is the relationship between a newly created class and a CPAN module class. The relationship could be: a) a standard class object is included in the newly created class (“has-a” relationship), and less frequently b) the newly created class inherits from the standard class (“is-a” relationship).

The code listing below is an example of a constructor for My class, which inherits from BaseClass. Moreover, My class contains AnotherClass object, which is private (the name begins with underline character — a convention not enforced by Perl itself).

Listing 1. My class: implementing relationships

package My;

require BaseClass; #required if BaseClass is present in BaseClass.pm
@ISA=qw(BaseClass);
use AnotherClass;
sub new {
my $package=shift;
my $self=$package->SUPER::new($package); #create object in BaseClass
$self->{_another_class_object}=new AnotherClass;
$self;
}

Listing 2. AbstractCGI class: implementing specific API (CGI)

package My;

require BaseClass; #required if BaseClass is present in BaseClass.pm
@ISA=qw(BaseClass);
use AnotherClass;
sub new {
my $package=shift;
my $self=$package->SUPER::new($package); #create object in BaseClass
$self->{_another_class_object}=new AnotherClass;
$self;
}



package AbstractCGI;
sub new {
my $package=shift;
my $self={
_cgi_method=>undef, #simple class data
_query=>undef
};
bless $self, ref $package || $package;
$self->_init;
$self;
}
sub _init {die} #private method
sub get_param {die}
sub is_print_form_mode { shift->get_param('mode') eq 'form' }
sub is_print_entries_mode { shift->get_param('mode') eq 'entries' }
2010-05-26T16:56:05+00:00 September 21st, 2004|CGI and Perl|0 Comments

About the Author:

Eugene Logvinov is a Web developer with the softPilot.2000 project (CONSUL Bureau, Sevastopol, Ukraine). The author is a third-year student at Kharkov National University (Ukraine), focused on new ideas in Web development and technical writing. You can contact him at rezonal [at] univer.kharkov.ua.

Leave A Comment