A module in Perl is a reusable piece of code that can be included in your scripts to provide additional functionality. Modules are typically used to encapsulate related subroutines, variables, and other code that can be shared across multiple scripts or applications.
Modules help in organising code, promoting code reuse, and maintaining a clean separation of concerns. They can be used to implement libraries, frameworks, or any reusable components that you might need in your Perl programs.
As your Perl programs grow in complexity, you will find that organising your code into modules can help keep things manageable. Modules allow you to encapsulate functionality and reuse code across different scripts.
A module in Perl is simply a package that contains subroutines and variables.
A package is defined using the package
keyword, and it typically corresponds to a file with the same name as the package, ending in .pm
. For example, a package named MyModule
would be in a file called lib/MyModule.pm
. It is good practice in perl to also name your modules with ::
instead of camel casing and then use sub directories to organize your code. For example, a module named My::Module
would be in a file called lib/My/Module.pm
. however this is not a strict requirement and you can use any file name as long as you specify the correct package name in your code. You will notice that the file path is relative to the lib
directory, which is a common convention in Perl projects also. The lib
directory is where you typically place your custom modules. To create a package, you would start your file with the following line:
package My::Module;
1;
This line defines a package named My::Module
. The package name should match the directory structure where the file is located, so if your file is in lib/My/Module.pm
, the package name should be My::Module
. We also include a 1;
at the end of the file, which is a common convention in Perl modules to indicate that the module has been loaded successfully. If the module does not return a true value, Perl will not load it.
We can 'use' a module in our Perl script by including it with the use
statement, followed by the module name. For example:
use My::Module;
This tells Perl to load the My::Module
package, which should be located in a file named lib/My/Module.pm
.
When you use a module, Perl will look for it in the directories specified in the @INC
array, which includes the current directory and any directories specified by the PERL5LIB
environment variable. If you have installed your module in a standard location, Perl will find it automatically. If you have not installed your module yet or installed it in a non standard location, you can add the directory to the @INC
array at runtime by using the use lib
pragma:
use lib 'lib';
or by setting the PERL5LIB
environment variable before running your script:
PERL5LIB=lib perl myscript.pl
Perl also has an extensive ecosystem of modules available on CPAN (Comprehensive Perl Archive Network), which is a repository of Perl modules that you can install and use in your projects. You can search for modules on CPAN and install them using tools like cpanm
or cpan
. If you have never visited metacpan then I would recommend you do https://metacpan.org/, it is a great resource for finding Perl modules and documentation. In the next post we will look at how to install a CPAN module as there is one non core dependancy which will be useful going forward.
In this post we will do all steps manually, we will create a simple Perl module and use it in a script. This will give you a basic understanding of how to create and use modules in Perl. So lets create your first module in Perl, first we need to create the directory structure for our module.
mkdir My-Module
cd My-Module
mkdir -p lib/My
touch lib/My/Module.pm
touch myscript.pl
Now, let's edit the lib/My/Module.pm
file to define our module, we will do a simple 'Hello World' module that prints a message when called:
# lib/My/Module.pm
package My::Module;
use strict;
use warnings;
sub hello {
print "Hello from My::Module!\n";
}
1; # Return true value to indicate successful loading
Next, let's edit the myscript.pl
file to use our module:
# myscript.pl
use strict;
use warnings;
use lib 'lib'; # Add the lib directory to @INC
use My::Module; # Use the My::Module package
My::Module::hello(); # Call the hello subroutine from My::Module
Now, you can run your script:
perl myscript.pl
You should see the output:
Hello from My::Module!
This demonstrates how to create a simple Perl module and use it in a script. You can expand your module by adding more subroutines, variables, and functionality as needed.
In future posts I will show you how to export functions from your module so that you can use them without the package name, I will also cover several approaches for object orientation and how to create more complex modules with additional features like overloading of operators. For now, this should give you a good starting point for creating and using modules in Perl.