Using Perl to Create Reusable Web Applications
By Eugene Logvinov2004-09-21
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;Listing 2. AbstractCGI class: implementing specific API (CGI)
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 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' }
Tutorial Pages:
» Object Technologies and HTML Templates in CGI Programming
» Object and Class Relationships
First published by IBM DeveloperWorks
| Related Tutorials: » Random subroutines in Perl » Log Script Use » Creating Perl Modules for Web Sites » Bit Vector, Using Perl Vec » Build a Perl/CGI Voting System » Perl Range Operator |
