Learning Perl - CPAN


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.

Related Blogs

Learning Perl – Introduction
Perl has long been known as the “duct tape of the Internet,” or "the Swiss Army chainsaw of scripting...
Learning Perl - Variables
I will attempt to explain things in this post in a way that is easy to understand, even for those who...
Learning Perl - Arrays
As stated in the previous post, Perl has three types of variables: scalars, arrays and hashes. Today...
Learning Perl - Hashes
In the last post we covered the basics of arrays, today we will look at hashes in more detail. What...
Learning Perl - Conditional Statements
So far we have covered basic variables in Perl, today we are going to look at how to use these...
Learning Perl - Loops and Iteration
In previous posts, we explored variables, arrays, hashes, and conditional statements in Perl. Now...
Learning Perl - Scalars
Before moving onto more complex topics lets come back to how we represent data in Perl. The most...
Learning Perl - References
In the last post we learnt how to create a reference to a scalar, an array, and a hash. In this post,...
Learning Perl - Ternary Operators
In a previous post, we learned about conditional statements in Perl. The ternary operator is an...
Learning Perl - Subroutines
Subroutines are one of the most important building blocks in programming. They allow you to organise...
Learning Perl - Regular Expressions
Regular expressions also known as regex or regexp, are a powerful way to match patterns in text. Most...
Learning Perl - Modules
A module in Perl is a reusable piece of code that can be included in your scripts to provide...
Learning Perl - Plain Old Documentation
When you write and program in any language it is good practice to document your code. Each language...
Learning Perl - Testing
In this post we will look at how to test Perl code using the Test::More module. Like documentation,...
Learning Perl - Exporting
In programming, we often want to share functionality across different parts of our code. In Perl,...
Learning Perl - Object Orientation
Object-Oriented Programming (OOP) is a widely used programming paradigm that enables the creation of...
Learning Perl - Inheritance
In the last post we discussed Object Oriented Programming in Perl, focusing on the basics of creating...
Learning Perl - File Handles
In programming file processing is a key skill to master. Files are essential for storing data,...
Learning Perl - Prototypes
Today we are going to discuss Perl subroutine prototypes, which are a way to enforce a certain...
Learning Perl - Overloading Operators
In the last post we investigated prototype subroutines in Perl. In this post, we will look at...