Home > Perl > Object Oriented Programming > Perl Packages - Part 4 (Functions)
Perl Packages - Part 4 (Functions)
Written by Philip L Yuson   
Who is this for
This article is for those who want to learn how to define and use functions in packages

What you need to know

Basic Perl Programming
Perl Packages
Perl Variable Scoping
Perl References

Introduction
In this article, we will discuss about functions in packages and also how to use packages. We talked about what packages are, how to limit the scope of variables in a package and references.

This month, we will see how you can use packages.

Functions
Functions are synonymous to subroutines. We will use the term Functions for our discussion.

We can create functions within packages to allow us to manipulate variables within the package. Although you can manipulate package variables outside of the package, it is 'cleaner' to manipulate these within the package. Keeping this rule in mind will be useful when we discuss about object-oriented programming in Perl.

A sample package can look like this:

package street; {

sub new {
my $self = {}; # create a hash reference
my ($class, $name) = @_; # get the parameters
$self->{name} = $name; # set hash ref element
return $self;
}
}

The package has one function called new. You can call the function whatever you want but to conform to standards, we will call this new.

And a sample script to use this package is this:

$a = street->new('Pembroke'); # create new street package
print "Package Type: ", ref($a), "\n"; #Print create result

The first line of the script calls the new function of the street package. The first thing the new function does is to allocate a variable ($self) that refers to a HASH. ($self is a hash reference).

The next line assigns the parameters passed to the function to the $class and $name variables respectively. The first parameter passed to a new is ALWAYS the package name, in this case street.

The next line assigns the value in the $name variable to the name element pointed to by the $self hash reference.

The last line in the function returns the value of the variable - $self.

On return to the main package, the result is stored in the $a variable. The next line prints the variable type referred to by the variable $a. Since $a has the contents of $self from the package, it refers to a HASH.

When you run this script, the result will be:

Package Name: HASH

Now for the interesting part
Of course, creating the package does not help us. So we need to do something useful - like print out the name of the street. To do this, we will have to revise the package to add the bolded line.

package street;	{
sub new {
my $self = {}; # create a hash reference
my ($class, $name) = @_; # get the parameters
$self->{name} = $name; # set hash ref element
bless $self, $class; # Bless the hash
return $self;
}
sub printstreet {# print the street name
$self = shift;
print "Street Name: $self->{name}\n";
}
}
Now, if you execute the script, the new function returns a reference to a street package instead of a HASH variable. This is seen by the output printed from the main package.

The last line of the script executes the printstreet function of the street package. For functions within a package, the first parameter is ALWAYS the reference to the hash it created on the new function. In our example, the printstreet function prints out the street element of the hash it created in the new function.

When you run this script, the result will be:

Package Name: street
Street Name: Pembroke

 

 
Copyright: © 2018 Philip Yuson