Using Perl Subroutines, Packages and Modules: How to Encapsulate, Organise and Reuse Code in Perl Applications

in #utopian-io8 years ago (edited)

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:

  • subroutines
  • packages
  • 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:

  • general mathematical formulae
  • 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:

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])}

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:

  • encapsulate useful code in a subroutine
  • group associated subroutines into a package
  • 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.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Congratulations @aminda! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the total payout received
Award for the number of upvotes received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Your contribution cannot be approved yet because it is not as informative as other contributions. See the Utopian Rules. Please edit your contribution and add try to improve the length and detail of your contribution (or add more images/mockups/screenshots), to reapply for approval.

You may edit your post here, as shown below:

You can contact us on Discord.
[utopian-moderator]

Your contribution cannot be approved because required edits were not made.

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.13
TRX 0.34
JST 0.036
BTC 109207.95
ETH 4399.20
USDT 1.00
SBD 0.84