Ubuntu

How to create and run a Perl script in Ubuntu 22.04 LTS

Perl is a simple and easy-to-learn programming language for beginners. Although Perl is an old-age computing language; however, it can still be used to develop large and complex programs as any structured programming language can do.

The Syntax of Perl is like that of the C programming language. The Per community has contributed more than 250000 software modules that are available on the CPAN (Comprehensive Perl Archive Network). Due to its large community and support, Perl scripts are widely used in Bioinformatics, Multi-tasking, Networking, GUI development, and so on. We have compiled this post to guide you how a Per script can be created and run on Ubuntu 22.04.

Prerequisites

The Perl script can be executed subject to the presence of Perl on Ubuntu 22.04. If you have not installed Perl on Ubuntu 22.04 then you must get the latest version of Perl from the Ubuntu’s official repository by using the following command:

$ sudo apt install perl

How to create and run a Perl script on Ubuntu 22.04

The Perl script can be created and run by using the command line application of Ubuntu 22.04. We have compiled a step-by-step procedure to create and run a Perl script on Ubuntu 22.04.

Step 1: Fire up the Ubuntu terminal (CTRL+ALT+T) and create a “.pl” file as follows:

$ touch test.pl

Step 2: You need to edit the newly created “.pl” file by using any text editor (nano or vim). We have used the nano editor to edit the “test.pl” file via the following command:

$ sudo nano test.pl

Once the file is opened in an editor, write the following line to tell your Ubuntu 22.04 to execute this file (test.pl) using the Perl interpreter:

#!/usr/bin/perl

Icon Description automatically generated

After that, we added a simple print statement in “test.pl“:

$message="Hello, Welcome to LinuxHint";

print("$message \n");

A picture containing text Description automatically generated

Once all is done, press “CTRL+S” and “CTRL+X” to save the changes and come out of the editor respectively.

Step 3: Try to run the Perl script “./test.pl” via the following command:

$ ./test.pl

Logo Description automatically generated with medium confidence

If any execution error occurs (as shown in the above output), you need to provide the execution privileges to the Perl script as follows as we did here with “test.pl“:

$ chmod +x test.pl

Use the following command again to execute the test.pl script.

$ ./test.pl

A screenshot of a computer Description automatically generated with medium confidence

Here you go!

Conclusion

A Perl script can be created by using any text editor on Ubuntu 22.04. You can run the Perl script from the terminal of Ubuntu 22.04. Perl is an open-source interpreted programming language that is well known in Linux-based operating systems because of its networking and multitasking support. Here, you have learned to create a Perl script and run it on Ubuntu 22.04.

About the author

Adnan Shabbir