Skip to main content

Introduction to Perl

Many of you have heard about Perl and wanted to learn it, but then wondered how to get started or what Perl is really used for. Thinking about these questions, I created this series to show you how to do some things in Perl, explain some useful functions, and cover other important topics.

This article is divided into three parts:

  • What is Perl?
  • What do you need to run Perl?
  • Introduction to basic concepts.

What is Perl?

The Perl programming language was developed in 1987 by Larry Wall and has served as the foundation for many modern programming languages like JavaScript, Ruby, and others. Perl is widely used by system administrators, hackers, crackers, and web developers. Its main strengths are:

  • Extremely fast at working with strings, arrays, hashes, and regular expressions;
  • Runs on over 100 different platforms;
  • Can be used for web development, often filling roles commonly taken by PHP, ASP, etc.;
  • Unicode support;
  • Supports both object-oriented and procedural programming;
  • Has a large community of developers who create modules to speed up programming work. These modules are available on CPAN;
  • Supports database integration;
  • Primarily designed as a scripting language for automating small tasks.

What do you need to run Perl?

To run Perl on your machine, you need to have Perl installed. On Unix and macOS systems, Perl usually comes pre-installed. You can check by opening the terminal and typing perl. If it’s not installed, follow the instructions for your OS.

On Windows, download and install Strawberry Perl. Once installed, Perl will be ready to use on your computer.

For programming in Perl, I recommend using Padre Perl (which runs on Linux and Windows), or alternatively, use Eclipse with the EPIC IDE plugin.

Introduction to basic concepts

Finally, we reach the practical part of this article. Here, I will show how to create a simple program that prints a message on the screen and another that asks for your name and prints a welcome message. Let’s get started!

After opening your IDE, write the following and save the file as helloworld.pl. The .pl extension indicates a Perl script. Copy this small script and run it in your IDE to see the output.

Now, you might be wondering what everything means — for you, it might look like gibberish: “use this, print that.” Don’t worry; it’s quite easy to understand, and I will explain step-by-step what everything does.

The first line is very important. It must be the very first line of the script with no preceding spaces or blank lines, or it will cause errors. When the Perl interpreter runs a file, it needs to know this is a Perl script and which interpreter to use, and that’s what this line tells it.

In this case, we use two pragmas — pragmas are extensions that add extra functionality not available by default. We include them using the use statement. Here, we use the warnings and strict pragmas. warnings alerts us about possible syntax or programming errors. strict enforces better organization and structure regarding syntax, variable declarations, subroutines, and more.

Finally, the print function outputs whatever we declare to the screen.

Now let’s create a small program that asks the user for their name and prints a welcome message on the screen.

In this script, I introduced five new elements:

  • my: Used to declare variables. It is mandatory when using the strict pragma. Without strict, it’s only mandatory inside functions;
  • $name: This is our variable. Variables in Perl are declared with a $ prefix;
  • : Reads input from the keyboard. Assigning this to $name means $name stores the user input;
  • \n: When used in a print, this adds a newline (like pressing Enter) after printing;
  • chomp: This is one of the most important functions — it removes trailing newlines from variable values, preventing unwanted line breaks in output;
  • Lastly, the print command calls the variable $name and prints its value.

Here’s a quick tip:

Instead of writing this, you can write this directly, which not only makes the code cleaner but also speeds up writing.