Using Perl Subroutines, Packages and Modules

How to Encapsulate, Organise and Reuse Code in Perl Applications

© Mark Alexander Bain

Jul 19, 2009
Using Perl Subroutines, Packages and Modules, Mark Alexander Bain
A Perl programmer should try to reuse code as much as possible (just as any programmer should), and they do that by using subroutines, packages and modules.

If there is one key to efficient programming (whether using Perl or any other programming language), it is to reuse code as much as possible. For the Perl programmer this means encapsulating useful (or at least often repeated) blocks of code into:

  1. subroutines
  2. packages
  3. modules

These can then be reused as required in any new Perl application.

Creating and Using Perl Subroutines

Perl actually sees no difference between subroutines and functions. Functions are simply a name for subroutines that return a value. They are both defined by using the sub key word. So, for example, the value of Pi could be set by using a subroutine:

$pi;
sub set_pi {$pi = 3.14159265358979}

This would then be called in the Perl script:

set_pi();
print $pi . "\n";

or the value could be returned from a function:

sub pi {return 3.14159265358979}

This could then be used to calculate values such as the area of a circle:

sub square {return $_[0]*$_[0]}
sub circle_area{return pi() * square($_[0])}

Here the subroutine will require an input (the radius of the circle). It is accessed as the first element of an array, and these elements are assigned when the subroutine is called:

$r = 2;
print circle_area($r) . "\n";

As the number of subroutines increase there will be ones that should be grouped together, and that's where Perl packages come in.

Creating and Using Perl Packages

A Perl package is used to group associated variables and subroutines. So, in the examples seen so far the obvious grouping would be:

  1. general mathematical formulae
  2. formulae related to circles

The package key word is used to group the items:

package simplemath;
sub square {return $_[0]*$_[0]}
sub pi {return 3.14159265358979 }

The programmer calls the subroutines by using a double colon (::) for example:

print simplemath::pi();

And these subroutines can be used within other packages:

package circle;
sub circumference {return 2*simplemath::pi()*$_[0]}
sub area {return simplemath::pi()*simplemath::square($_[0])}

Here two packages have been created (simplemath and circle) each containing two subroutines. One big advantage is that each package can contain subroutines with the same name but which do different things:

package rectangle;
sub area {return $_[0]*$_[1]}
sub perimeter {return 2*($_[0]+$_[1])}

The programmer now has a well defined set of subroutines which can be run in a script (the output of which can be seen in figure 1 at the bottom of this article):

$r = 3;
print circle::circumference($r) ."\n";
print circle::area($r) . "\n";
$a = 4;
$b = 5;
print rectangle::area($a, $b) . "\n";
And there is one final level of encapsulation - the Perl Module: Creating and Using Perl Modules A Perl module is simply a Perl package placed in its own file. So, for example the simplemath package shown above could be placed in the file "simplemath.pm". However, the file will need an addition. The last line of the file must be:
1;
This is so that the file always returns a value when it is loaded:
use simplemath;
Each of the packages can be loaded into its own module, for example "circle.pm" would be:
package circle;
use simplemath;
sub circumference {return 2*simplemath::pi()*$_[0]}
sub area {return simplemath::pi()*simplemath::square($_[0])}
1;
And the final code for the script would be:
use simplemath;
use circle;
use rectangle;
$r = 3;
print circle::circumference($r) ."\n";
print circle::area($r) . "\n";
$a = 4;
$b = 5;
print rectangle::area($a, $b) . "\n";
In this way the Perl programmer can:
  1. encapsulate useful code in a subroutine
  2. group associated subroutines into a package
  3. store individual packages in their own file (or module)
And, of course, this code can then be reused in every application that the programmer goes on to create.

The copyright of the article Using Perl Subroutines, Packages and Modules in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish Using Perl Subroutines, Packages and Modules in print or online must be granted by the author in writing.


Using Perl Subroutines, Packages and Modules, Mark Alexander Bain
Figure 1: The Perl Associative Array, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo