|
||||||
Using Perl Subroutines, Packages and ModulesHow to Encapsulate, Organise and Reuse Code in Perl ApplicationsA 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:
These can then be reused as required in any new Perl application. Creating and Using Perl SubroutinesPerl 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 PackagesA Perl package is used to group associated variables and subroutines. So, in the examples seen so far the obvious grouping would be:
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";
1;
use simplemath;
package circle;
use simplemath;
sub circumference {return 2*simplemath::pi()*$_[0]}
sub area {return simplemath::pi()*simplemath::square($_[0])}
1;
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";
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.
|
||||||
|
|
||||||
|
|
||||||