DEV Community

Cover image for Learning Perl - CPAN
LNATION for LNATION

Posted on • Edited on

Learning Perl - CPAN

In the last post I showed you how to create a new module and how to use it in your code. In this post I will show you how to download and install a module from CPAN.

The Comprehensive Perl Archive Network (CPAN) is a large collection of Perl modules and distributions. It is the primary source for Perl modules and is widely used by the Perl community. You can find documents, tutorials, and other resources on the CPAN website: https://www.cpan.org/. To find module documentation, you can use the CPAN search engine: https://metacpan.org/.

To install a module from CPAN, you can use the cpan command-line tool. This tool is included with Perl and allows you to download and install modules from CPAN. To install a module, you can run the following command in your terminal:

cpan Module::Name
Enter fullscreen mode Exit fullscreen mode

Today we are going to install a module called Module::Starter. This module provides a simple way to create a new Perl module with a basic structure and boilerplate code. It is a great tool for starting new Perl projects and will safe us a lot of time over the following posts. You can find the documentation for Module::Starter on metacpan https://metacpan.org/pod/Module::Starter.

So to install Module::Starter, run the following command:

cpan Module::Starter
Enter fullscreen mode Exit fullscreen mode

If you are running this command for the first time, it may prompt you to configure CPAN. You can usually accept the default settings by pressing Enter. It will ask you a few questions about your configuration, such as whether you want to use the default mirror for downloading modules. It will ask if you want to configure 'Local::Lib' to install modules in your home directory, which is a good option if you do not have root access or do not want to install modules system-wide. Do take note of any output messages especially surrounding Local::Lib, if it asks you to export environment variables, you should do so as it will help your programs find the installed modules in the future. After the configuration is complete, CPAN will proceed to download and install the Module::Starter module along with any dependencies it may have.

As a last resort use sudo to install the module system-wide:

sudo cpan Module::Starter
Enter fullscreen mode Exit fullscreen mode

Once the installation is complete, you can verify that the module is installed by running:

perl -MModule::Starter -e 'print $Module::Starter::VERSION, "\n";'
Enter fullscreen mode Exit fullscreen mode

This command will print the version of the Module::Starter module that is installed on your system. If you see a version number, it means the installation was successful.

Now that you have Module::Starter installed, you can use it to create a new Perl module. In the next post, I will show you how to use Module::Starter to create a new module with a basic structure and boilerplate code. We will then talk about documentation as this is important to future proofing your code.

Top comments (0)