Steps to Setup & Run a Perl Script on Ubuntu 20.04 LTS

Hello, In this blog we are discussing how to install Perl & run a perl script on Ubuntu. Perl is a free & open source high level interpreted and dynamic programming language & used for generating SQL queries. It supports both the procedural and Object-Oriented programming. It also used for CGI( Common Gateway Interface) scripts, in web development, & GUI(Graphical User Interface) development.

There are some steps to install Perl on Ubuntu:

Step 1: Update the System.

apt-get update

Step 2: Install Perl on system.

apt-get install perl

  • Here is the command output.

root@ip-172-31-31-123:/home/ubuntu# apt-get install perl
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
git-man
Use 'sudo apt autoremove' to remove it.
Suggested packages:
perl-doc libterm-readline-gnu-perl | libterm-readline-perl-perl make libb-debug-perl liblocale-codes-perl
The following NEW packages will be installed:
perl
0 upgraded, 1 newly installed, 0 to remove and 32 not upgraded.
Need to get 224 kB of archives.
...
Unpacking perl (5.30.0-9ubuntu0.2) ...
Setting up perl (5.30.0-9ubuntu0.2) ...
Processing triggers for man-db (2.9.1-1) ...

  • To list the installed perl packages.

apt list --installed | grep -i perl

  • Here is the command output.

root@ip-172-31-31-123:/home/ubuntu# apt list --installed | grep -i perl
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
liblocale-gettext-perl/focal,now 1.07-4 amd64 [installed,automatic]
libperl5.30/focal-updates,focal-security,now 5.30.0-9ubuntu0.2 amd64 [installed,automatic]
libtext-charwidth-perl/focal,now 0.04-10 amd64 [installed,automatic]
libtext-iconv-perl/focal,now 1.7-7 amd64 [installed,automatic]
libtext-wrapi18n-perl/focal,now 0.06-9 all [installed,automatic]
perl-base/focal-updates,focal-security,now 5.30.0-9ubuntu0.2 amd64 [installed,automatic]
perl-modules-5.30/focal-updates,focal-security,now 5.30.0-9ubuntu0.2 all [installed,automatic]
perl/focal-updates,focal-security,now 5.30.0-9ubuntu0.2 amd64 [installed]
python3-hyperlink/focal,now 19.0.0-1 all [installed,automatic]

  • Check perl version.

perl -v

  • Here is the command output.

root@ip-172-31-31-123:/home/ubuntu# perl -v
This is perl 5, version 30, subversion 0 (v5.30.0) built for x86_64-linux-gnu-thread-multi
(with 50 registered patches, see perl -V for more detail)
Copyright 1987-2019, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.

Step 3: Install the Perl SQL module.

apt install libdbd-mysql-perl

  • Here is the command output.

root@ip-172-31-31-123:/home/ubuntu# apt install libdbd-mysql-perl
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
git-man
Use 'sudo apt autoremove' to remove it.
The following additional packages will be installed:
libdbi-perl libmysqlclient21 mysql-common
Suggested packages:
libclone-perl libmldbm-perl libnet-daemon-perl libsql-statement-perl
The following NEW packages will be installed:
libdbd-mysql-perl libdbi-perl libmysqlclient21 mysql-common
0 upgraded, 4 newly installed, 0 to remove and 32 not upgraded.
Need to get 2111 kB of archives.
After this operation, 10.1 MB of additional disk space will be used.
Do you want to continue? [Y/n] y

Step 4: Run a Script.

  • Create a file.

vim test.pl

  • Add the following lines:

#!/usr/bin/perl
use warnings;
print("Hello, this is test script");

  • Run the Script.

Syntax:

perl file-name.pl

For Example:

perl test.pl

  • Here is the command output.

root@ip-172-31-31-123:/home/ubuntu# perl test.pl
Hello, this is test script

Example:

  • Add the following lines in .pl file.

#!/usr/bin/perl
$b = 10; # Assigning value to $b
$c = 30; # Assigning value to $c
$a = $b + $c; # Performing the operation
print "$a";

  • Run the file.

perl test.pl

  • Here is the output.

root@ip-172-31-31-123:/home/ubuntu# perl test.pl
40

Leave a Reply