A very simple first example
We'll begin with an extremely simple example. We want to have a subroutine that returns the number of days in a specified year. Rather than put this subroutine into lots of different codes, we want to make a module, and then make use of this module in all our codes.
Firstly, we make a file called Example.pm containing the following code:
package Example;The package name is Dates and has a single subroutine daysInYear that returns the days in a year. Note that the last line must return true.
sub daysInYear
{
my $year = shift;
if ($year % 4 == 0)
{
if ($year % 100 == 0 && $year % 400 != 0)
{
return(365);
}
else
{
return(366);
}
}
else
{
return(365);
}
}
1; # do not forget this line!
Here's a simple example showing how to use this package:
#!/usr/bin/perlHere we assume this Perl program and Example.pm are in the same directory.
use Example;
$days = Example::maxDaysInYear(2004);
print "days in 2004 were: $days\n";
This is the simplest way to make re-usable code. You just need to put all your frequently-used subroutines into modules (.pm files) with appropriate package names, and then call them from all your Perl programs.
Creating and using classes
Now we consider the following module, called Book.pm, which stores information about a book:
package Book;In this example we are making a class called Book. In order to create objects, we need to have a constructor method that returns a new object rather than just a normal data type. Here we define an object constructor called new (although this can have any name you want, new is most frequently used).
sub new
{
my $class = shift;
my $self = {
_title => shift,
_author => shift,
_publisher => shift
};
bless $self, $class;
return $self;
}
sub getTitle
{
my ($self) = @_;
return $self->{_title};
}
sub getAuthor
{
my ($self) = @_;
return $self->{_author};
}
sub getPublisher
{
my ($self) = @_;
return $self->{_publisher};
}
1;
The line containing bless is important. bless is usually called as the last action in a constructor method, and is used in the following ways:
bless REFThis tells the entity referenced by REF that it is now an object in the CLASSNAME package. If CLASSNAME is omitted, it tells the entity referenced by REF that it is now an object in the current package.
bless REF, CLASSNAME
In this module we have also defined 3 methods: getTitle, getAuthor and getPublisher, which are simple subroutines that return the title, author and publisher respectively. These subroutines enable you to access the data in the Book class.
This module can be used in the following way:
#!/usr/bin/perlInheritance
use Book;
$object = new Book("Principles of Modern Chemistry",
"John Citizen", "Smith Books");
$title = $object->getTitle();
$author = $object->getAuthor();
$publisher = $object->getPublisher();
print "title = $title\n";
print "author = $author\n";
print "publisher = $publisher\n";
Suppose we want a new class Chapter. We will want to know the title, author and publisher of the book that the Chapter comes from. It thus makes sense to inherit Book. Contents of Chapter.pm:
package Chapter;This creates a derived class called Chapter which inherits from the base class and does nothing else.
use Book;
@ISA = ("Book");
1;
How to we add extra methods to the subclass, for example a method getText which returns text. We just need to add the following to Chapter.pm:
sub getTextWe'll also need to modify the Chapter constructor (we of course don't want to add the text data to Book, as this is something that should only be in Chapter). We can add the following to Chapter.pm:
{
my ($self) = @_;
return $self->{_text};
}
sub new
{
my $class = shift;
my $self = $class->SUPER::new();
$self->{_text} = $_[3];
bless $self, $class;
return $self;
}To summarize, we have created a class called Book which contains information about the a specific book. We also created a derived class called Chapter which inherits Book, and in addition contains additional data and an additional method, not present in Book.
No comments:
Post a Comment