|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Camel POOPMost people are not aware of the fact that Perl has support for object-oriented programming. If you've used another object-oriented programming language such as Java or C++ or been exposed to object-orientation, then object oriented programming in Perl is nothing like that. To do real useful object-oriented programming in Perl, you only need to use three simple rules as put forth by Larry Wall in Object Oriented Perl. Object oriented programmers are familiar with the concept of object and classes, but I will review that here quickly. An object is a thing that provides access to or modification of data. A class is a description of the attributes of a particular kind of object and the manner in which those objects can be accessed and modified. A method is a means by which an object's data is accessed or modified. An object is an instance of a class. An example would be a Package deliveryTo create a class in Perl, we first build a package. A package is a self-contained unit of user-defined variables and subroutines, which can be re-used over and over again. They provide a separate namespace within a Perl program that keeps subroutines and variables from conflicting with those in other packages. To declare a class named package Person;
That's it. The scope of the package definition extends to the end of the file, or until another There's a method to this madnessA method is a means by which an object's data is accessed or modified. In Perl, a method is just a subroutine defined within a particular package. So to define a method to print our sub print {
my ($self) = @_;
#print Person info
printf( "Name:%s %s\n\n", $self->firstName, $self->lastName );
}
The subroutine $khurt->print();
When the object method is invoked, a reference to the object is passed in along with any other arguments. This is important since the method now has access to the object on which it is to operate. How do we create the invoking object? Bless me fatherTo create an instance of a class (an object) we need an object constructor. This constructor is a method defined within the package. Most programmers choose to name this object constructor method One can use any kind of Perl variable as an object in Perl. Most Perl programmers choose either references to arrays or hashes. Let's create our constructor for our Person class using a #constructor
sub new {
my $self = {
_firstName => undef,
_lastName => undef,
_ssn => undef,
_address => undef
};
bless $self, 'Person';
return $self;
}
What have we done? We created a subroutine called To create an instance of our my $khurt = new Person();
We have not defined accessor methods or done any error checking on the input values or keys or the anonymous hash reference, but we have the start of a Perl #constructor
sub new {
my ($class) = @_;
my $self = {
_firstName => undef,
_lastName => undef,
_ssn => undef,
_address => undef
};
bless $self, $class;
return $self;
}
Other object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and so provide accessor methods to modify object data. Perl does not have private variables but we can still use the concept of accessor methods and ask programmers to not mess with our object innards. For our #class Person
package Person;
use strict;
use Address; #Person class will contain an Address
#constructor
sub new {
my ($class) = @_;
my $self = {
_firstName => undef,
_lastName => undef,
_ssn => undef,
_address => undef
};
bless $self, $class;
return $self;
}
#accessor method for Person first name
sub firstName {
my ( $self, $firstName ) = @_;
$self->{_firstName} = $firstName if defined($firstName);
return $self->{_firstName};
}
#accessor method for Person last name
sub lastName {
my ( $self, $lastName ) = @_;
$self->{_lastName} = $lastName if defined($lastName);
return $self->{_lastName};
}
#accessor method for Person address
sub address {
my ( $self, $address ) = @_;
$self->{_address} = $address if defined($address);
return $self->{_address};
}
#accessor method for Person social security number
sub ssn {
my ( $self, $ssn ) = @_;
$self->{_ssn} = $ssn if defined($ssn);
return $self->{_ssn};
}
sub print {
my ($self) = @_;
#print Person info
printf( "Name:%s %s\n\n", $self->firstName, $self->lastName );
}
1;
Making babiesObject-oriented programming sometimes involves inheritance. Inheritance simply means allowing one class called the # class Employee
package Employee;
use Person;
use strict;
our @ISA = qw(Person); # inherits from Person
What we have done is load the #create Employee class instance
my $khurt = new Employee();
#set object attributes
$khurt->firstName('Khurt');
$khurt->lastName('Williams');
without any other changes. Now let's add some methods. # class Employee
package Employee;
use Person;
use strict;
our @ISA = qw(Person); # inherits from Person
#constructor
sub new {
my ($class) = @_;
#call the constructor of the parent class, Person.
my $self = $class->SUPER::new();
$self->{_id} = undef;
$self->{_title} = undef;
bless $self, $class;
return $self;
}
#accessor method for id
sub id {
my ( $self, $id ) = @_;
$self->{_id} = $id if defined($id);
return ( $self->{_id} );
}
#accessor method for title
sub title {
my ( $self, $title ) = @_;
$self->{_title} = $title if defined($title);
return ( $self->{_title} );
}
sub print {
my ($self) = @_;
# we will call the print method of the parent class
$self->SUPER::print;
$self->address->print;
}
1;
Looking at the code, you will notice that we have a Putting it togetherSo now that we have a complete set of classes, we can write a small program to test them. use strict;
use warnings;
use diagnostics;
use Employee;
#create Employee class instance
my $khurt = eval { new Employee(); } or die ($@);
#set object attributes
$khurt->firstName('Khurt');
$khurt->lastName('Williams');
$khurt->id(1001);
$khurt->title('Executive Director');
$khurt->address( new Address() );
$khurt->address->street('10 Anywhere Lane');
$khurt->address->city('Anytown');
$khurt->address->state('NJ');
$khurt->address->zip('12345');
#diplay Employee info
$khurt->print();
Let's execute our code and see the output: $ ./test.pl
Name:Khurt Williams
Address:10 Anywhere Lane
Anytown, NJ 12345
It works! We covered the basics of object oriented programming in Perl and I hope this article was informative and useful.
|
||||||||||||||||||||||