Skip to main content

Using Perl to create reusable Web apps

Object technologies and HTML templates in CGI programming

Eugene Logvinov (rezonal@univer.kharkov.ua), Web developer, softPilot.2000 team
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@univer.kharkov.ua.

Summary:  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.

Date:  01 Jun 2001
Level:  Introductory
Activity:  997 views

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' }


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@univer.kharkov.ua.

Comments (Undergoing maintenance)



Trademarks  |  My developerWorks terms and conditions

Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Linux, Open source
ArticleID=11124
ArticleTitle=Using Perl to create reusable Web apps
publish-date=06012001
author1-email=rezonal@univer.kharkov.ua
author1-email-cc=

My developerWorks community

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Rate a product. Write a review.

Special offers