# Perl Programming/Code reuse (modules)
## Overview

Perl modules (files that end with the pm extension) are files of Perl code that can be reused from program to program. There is an online repository of Perl modules called CPAN (Comprehensive Perl Archive Network) at http://cpan.org. Many of these modules come standard with Perl, but others must be installed as needed.
There are thousands of Perl modules that do everything from creating a temporary file to calling Amazon web services. These modules can make it easy to quickly write your application if you know how to find, install, and use the appropriate Perl modules. If you are thinking of writing your own Perl module, the best thing to do is to first search at http://Search.cpan.org to make sure you are not about to reinvent the wheel.
There are two major styles of Perl modules:
* Functional
* Object-oriented
Some perl modules use both approaches.
A functional Perl module might get used like this:
To use an object-oriented Perl module, you would do something like this:

### How to install a Perl module

Find the Perl module, you want at http://cpan.org, and download the gzipped file. Untar and unzip the file:
Then change into this the directory and follow the instructions in the README or INSTALL file.
You can also use a command-line program called cpan, if you have it installed:

### To write your own Perl module

Perl modules differ from Perl scripts in two key and simple ways. Instead of starting the module with "#!/path/to/perl", you start the file with the following:
You need to end the module with a true value, so the common practice is to do this at the end of the file:
The following is a valid Perl module:

#### Example

We create a new file called ExampleModule.pm, and in it have the following code:
We can test to see if the syntax is valid by running:
It will print out "ExampleModule.pm syntax OK", if all is well. Otherwise, you can debug using the messages that are printed out.
Now we can use it in a script to see if it works:
Voilá! You have made a Perl module.

### Create a CPAN-style Perl module

CPAN-style modules have test suites and a way to build the module into the Perl library.
Download and install: Module::Starter from CPAN. Once this is installed, there will be a program called module-starter in your path. To create a new module, do the following from the command line:
It will then create a set of directories for you, including some shell module files with starter POD documentation. The Perl modules will be inside the lib directory inside the directory that is created. These are the files to edit. You can put your tests for the modules into the "t" directory. To install and build the module, you do the following:

### How to pass parameters to a function in a Perl module

When calling a function from a Perl module, the module name, function name and opening and closing parenthesis are used. If some parameters are to be passed, this is done in due order inside the parenthesis. The code inside the module gets the parameters the following way:
After the first line, the function count can use the passed parameters $command_type and $rc. The main program calls the count procedure as follows: