How to start using Perl on Linux

In this quick note I am going to investigate how to get started with Perl on Linux.

It’s worth saying that the phrase “getting started with Perl” is pretty general. But since I’m a newbie to the subject, I’m going to have to start at the beginning.

What is Perl?

Let’s fast forward through this section.

To find out what Perl is, I can check its manual with the command:

man perl

And that same manual tells me:

NAME
perl – The Perl 5 language interpreter

PERL MANUAL

This is that Perl 5 is a programming language… well that’s not much help, I can say that I still don’t understand anything.

Luckily the manual goes ahead and says:

Perl officially stands for Practical Extraction and Report Language, except when it doesn’t.

PERL MANUAL

Well, at least now I know that the name Perl means “Practical Extraction and Report Language” (or at least that’s what it means from time to time, according to the text).

To finish this ultra-summary, Perl is a programming language that started with the goal of working with text documents. Completing reports and stuff, if the name says anything. Then it expanded to become a general-purpose language.

Although there is something more interesting I can take away from trying all this. A few minutes ago I was able to run the “man perl” command in the terminal, and usually that can only be done when the relevant packages and dependencies are already installed.

That makes me think that what I need to use this language is already on the computer, However, to comply with the formalities I can answer ….

How to install Perl on Linux?

The good thing is that Perl is already included in most Linux distributions.

I can check what version of Perl is on my operating system with the command:

perl -v

And just for the formality of including a way to install Perl, I can do it for example with apt:

apt install perl

And if I already have the dependencies installed, this last command will also tell me what version it is.

A first Perl program

Making a first Perl program is incredibly easy, especially if you have some knowledge of the Bash language.

And if this is the first time you are reading this site, let me tell you that I started writing all this in order to learn how to program in Bash.

Now if I take into account that we can use artificial intelligence or LLM to investigate all this, a first program has to be doubly easy then.

To start, I go to the directory where I want to save the program. I am going to create a file with the extension “.pl” to start writing.

touch example.pl

Yes, the name of the program is not very original but who has time to give a name that describes what the program really does.

Then I write the code like this:

#!/usr/bin/perl

print "\n\nThis is an example program to use Perl\n\n";

That’s it, more or less. To run it I type in the terminal:

perl example.pl

With that I already have my message in the terminal, and my first program in Perl finished. It’s not much, but it’s a start.

An unhelpful comparison

If the code above looks a lot like a Bash program, it is because it really looks like one. Let’s think about the same program written in Bash:

#!/bin/bash
echo -e "\nThis is an example program to use Bash\n"

Whereas in Bash it started with what is known with a “Shebang” like this:

#!/bin/bash

To specify to the system that Bash has to be used to interpret the code, in Perl I start the program with:

#!/usr/bin/perl

With the same goal of directing the system to the correct directory.

Then to write the message in the terminal I did:

print "\n\nThis is an example program to use Perl\n\n";

Using “Print” as I do in other programs such as Python.

And the addition of “\n” at the beginning and end of the text inserts an empty line at the beginning and end of the text.

While to achieve the same in Bash I did:

echo -e "\nThis is an example program to use Bash\n"

Some first differences. In this case I used “echo” instead of “Print”. And the addition of the “-e” option allows the addition of the new lines, because I want to present the text in a way easier to understand in the terminal.

How to write comments in Perl

This is as good an opportunity as any to learn how to place comments in our code.

Let’s remember that comments allow us to include information inside the program to make it easier to understand. This information is not part of the code, that is to say that the program is not going to interpret it so to speak.

To continue with my strange theme of “comparing code written in Perl with code written in Bash”, I can mention the following:

– this way I can create a single line comment in Bash:

# Bash comment line

– and this way I can create a single line comment in Perl:

# Perl comment line

This is good, in both languages you can write a comment in the same way. We already know two things, knowing only one.

Regarding creating a multi-line comment in Perl, it seems to be possible in this way:

=pod

This is a comment.
It has multiple lines.
3 in total

=cut

It can be well understood then that all that framed between the “=pod” and “=cut” commands is our comment.

Now, regarding the issue of writing multi-line comments in Bash… I really have no idea how to make those comments. To solve it I simply write several lines using the single line comment technique featured before until I create a paragraph.

Conclusion

I hope I have covered in this note as concisely as possible how to get started with Perl on Linux.

Getting started is not particularly difficult… but continuing to use it is not particularly easy either.

I hope to find some projects to be able to put all this info into practice, and thus expand my knowledge in this language.

I will appreciate your help if you leave me a message, especially if you find errors in the text. That way I will be able to fix it.

We’ll follow it in the next note.