How to run a Perl script on Linux

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to run a perl script on your linux terminal.

Perl is a programming language, that is been around for a long time.

Perl is free and open source, which means you can contribute to Perl development, or inspect its code.

In this tutorial, we are using Linux Mint.

Perl is already installed on Linux Mint, so you don't need to install it manually in order to run a perl script.

Running a perl script on Linux is quite simple, and it can be done just like running scripts in other languages such as : bash, python etc.

Step 1 - Check if Perl is installed

Even though Perl is included by default on all Linux distributions, you need to check if perl is installed on your machine, before running a perl script.

To check if Perl is installed, you can easily do so by checking Perl version.

Perl version can be checked directly from your terminal, so to do so run the command below.

perl -v

And it should display the version of Perl that is installed on your system. For example, we are running perl 5 version 34.

If Perl is not installed then the command above will throw an error, and you can install Perl by running the commands below one by one.

wget https://www.cpan.org/src/5.0/perl-5.38.2.tar.gz
tar -xzf perl-5.38.2.tar.gz
cd perl-5.38.2
./Configure -des -Dprefix=$HOME/localperl
make
make test
make install

Step 2 - Run a Perl script

To run a perl script via the terminal is quite simple, but before doing that, let's write a simple perl script that we can run.

1) Create the perl script called script.pl

sudo nano script.pl

2) Write the following perl code into the file that you created.

#!usr/bin/perl

 
use Term::ANSIColor;

print color("green"), "Hello " , color("yellow") , "World\n", color("reset");

💡
Note: We are using the Term::ANSIColor module, which allows us to change the text color.

If you don't want to use the Term::ANSIColor module on your perl script, then you can easily remove it, and run code without it.

The perl script without Term::ANSIColor should look like this.

#!usr/bin/perl

 

print("Hello, World\n");

3) Run the perl script.

perl script.pl

The perl code above will print "Hello world" in two different colors. Hello will be green, and World will be yellow as shown in the screenshot below.

To change the colors, simply replace green and yellow with yours.

Conclusion

In this tutorial, you learned how to check if perl is installed, how to install it when Perl is not installed, and also how to run a perl script on your terminal.