How to install Perl in Ubuntu?

Perl is a general-purpose, high-level, interpreted programming language. It is the acronym of Practical Extraction and Reporting Language, initially, Perl was designed as a scripting language to make report processing easier.

Perl is used in web development for writing server-side tasks and interface the frontend to the database. It was developed by Larry Wall in 1987.

In this article, I will discuss how to install and use Perl in Ubuntu.

Prerequisites

To follow this guide you should have the following.

  • A system running on a recent version of Ubuntu Linux
  • Access to a user account that has sudo privileges

How to install Perl in Ubuntu

Follow the steps below to install Perl in Ubuntu or in Linux distribution which is based on Ubuntu such as Linux Mint.

Update your system

Before going through the steps to install Perl in your system make sure to run the update command to refresh the local package repository and upgrade all packages to a newer version.

sudo apt update && sudo apt upgrade -y

Installing Perl

Once your system gets updated, use the following command to install Perl in your system.

sudo apt install perl

If asked for confirmation press y and then enter. This will install all the required packages to set up Perl on your system.

Check installed Perl version

You can verify Perl installation by checking its version after installing it on your system.

perl -v

perl version

Write your first Perl script

You have set up Perl on your system now you can write your first script in Perl. Here we will see the example of the hello world program.

Use the following command to create a Perl file (with .pl extension) –

sudo nano hello.pl

Add the following code to this file.

#!/usr/bin/perl
use warnings;
print("Hello, World!\n");

Save the file by pressing ctrl+s and exit from the editor by using ctrl+x.

Now execute this script by using the given command.

perl hello.pl

You can see the output of this script in the image below.

perl script execute

Installing a Perl module

 Perl module is a collection of related subroutines and variables that can perform a set of programming tasks. This increases the reusability of Perl code. For example, you can install the MySQL module for Perl by using the given command.

sudo apt install libdbd-mysql-perl -y

You can use CPAN (Comprehensive Perl Archive Network) that features a wide range of Perl modules to install these modules.

Conclusion

You have successfully installed Perl in your system. Now if you have a query related to this you can write us in the comments below.

Leave a Comment