5.6.3 Each Class Definition in a File of Its Own
5.6.3 Each Class Definition in a File of Its Own
Of course, instead of having all the class definitions and the main program in just one file as in the previous program, we can write each class definition in a file of its own. Below, we rewrite the program given in the beginning of this section. Now, we have two module files: Person.pm and Friend.pm. We also have a “main” program in a file called allfriends.pl. First, we show the Person.pm file containing the Person
module.
Program 5.17
#file Person.pm
use strict;
package Person;
#Since there are no variables to export, it is not necessary to use the Exporter
#module and the package-global variable @EXPORT
sub make{ #A method to create an instance of class Person
my ($self, $class);
$self ={}; #Initialize $self to reference an empty hash.
$class = $_[0]; #This is the class of the instance to create
$self -> {name} = undef; #Set the `name' field to an undefined value
bless ($self, $class); #$self is `blessed' to become an instance of $class
return $self;
}
sub set_name { #A method to assign the name field
my $self = $_[0];
$self -> {name} = $_[1];
}
sub get_name{ #A method to access the name field
my $self = $_[0];
my $name = $self -> {name};
return $name;
}
1;
As usual, this module defines a constructor method called make, and two additional methods set_name and get_name for the class Person. Note that we do not use the mechanism we discussed earlier for exporting any names or identifiers. Of course, there are no variables or fields in the module to export. Method names need not be exported. A method is called on an object instance of a specific class, and Perl knows where to find the definition.
Next, we show the Friend.pm file containing the Friend module.
Program 5.18
#file Friend.pm
use strict;
package Friend;
use Person;
use Exporter;
use vars ('@ISA', '@EXPORT');
@ISA = ("Person", "Exporter"); #A `Friend' is a subclass of class `Person'
#Since no variables are exported, it is not really necessary to use
#the Exporter class and the package-global @EXPORT variable.
#Subroutines are available outside a package without being exported
#when used with an object instance.
@EXPORT = (); #Here, it's set to the empty list.
sub make { #A constructor method for class `Friend'
my $self = {}; #Class `Friend' has two fields and inherits one from
my $class = $_[0]; #parent class `Person'
$self -> {hometown} = undef; #Set the two fields to `undef'
$self -> {age} = undef;
bless ($self, $class); #Person has no parents
}
sub set_hometown {
my $self = $_[0];
$self -> {hometown} = $_[1];
}
sub get_hometown{
my $self = $_[0];
return $self -> {hometown};
}
sub set_age {
my $self = $_[0];
$self -> {age} = $_[1];
}
sub get_age{
my $self = $_[0];
return $self -> {age};
}
1;
The Friend module defines the class Friend. Friend is a sub-class of the class Person defined as module Person in the file Person.pm. All files are in the same directory. This module defines a constructor method make, and four additional methods:
set_hometown, get_hometown, set_age and get_age. A module needs to return a true value when used. That is why 1; is the last statement of a module. It is always true.
The main program is called allfriends.pl and is given below.
Program 5.19
#!/usr/bin/perl
#allfriends.pl
use strict;
use Friend;
#Create two instances of Person, and two instances of Friend
my ($erin, $ron, $jeff, $tommy);
$erin = Person -> make; $erin -> set_name ("Erin");
$ron = Person -> make; $ron -> set_name ("Ron");
$jeff = Friend -> make; $jeff -> set_name ("Jeff");
$jeff -> set_hometown ("Boulder");
$jeff -> set_age (21);
$tommy = Friend -> make; $tommy -> set_name ("Tommy");
$tommy -> set_hometown ("Washington DC");
$tommy -> set_age (18);
#Print information contained in the object instances
printf "%-7s\n", $ron -> get_name;
printf "%-7s\n", $erin -> get_name;
printf "%-7s%-15s%d\n", $jeff -> get_name,
$jeff -> get_hometown, $jeff -> get_age;
printf "%-7s%-15s%d\n", $tommy -> get_name,
$tommy -> get_hometown, $tommy -> get_age;
Note that a module cannot be executed, only the program allfriends.pl can. The output of this program is the same as before, but is repeated below.
Ron Erin Jeff Boulder 21 Tommy Washington DC 18
