WhatIs.com

Perl

By Ben Lutkevich

What is Perl?

Perl is a family of script programming languages that is similar in syntax to the C language. It is an older, open source, general use, interpreted language.

Perl was developed with usability in mind. Its efficient design lets developers do a lot with a little bit of code. Its nickname is the "Swiss Army chainsaw" of the internet.

Perl is simpler to learn and code with than more structured languages, such as C and C++. Nevertheless, the language is used to develop advanced programs. The language is often used to develop common gateway interface (CGI) programs because it has good text manipulation capabilities and the ability to deal with binary files.

Design and features of Perl

Perl includes popular Unix facilities, such as sed, awk and tr. As an interpreted language, code can run as is, and instructions are executed without first compiling the entire program into machine language instructions.

However, Perl can be compiled before execution into C code or cross-platform bytecode. When compiled, a Perl program is almost as fast as a fully pre-compiled C language program. A plugin is available for some servers, such as Apache, loading Perl permanently in memory. This reduces compile time and results in faster execution time of CGI Perl script.

Here are additional Perl features:

Perl syntax and code examples

Like all programming languages, Perl follows a basic syntax for writing code. It contains keywords and variables for storing expressions and statements that execute the program's logic.

A Perl program always begins with this line of code:

#!/usr/bin/perl

This is the shebang line. It lets the computer know that the following text will be Perl code and calls the Perl interpreter. Perl programs must pass through the interpreter to execute.

After that, a user could write a basic "Hi there, world!" script:

print "Hi there, world!\n"

The output would be the following:

"Hi there, world!"

Users could also declare a variable. In Perl, the variable is predefined by a variable indicator -- a $, @ or %. These define the data type:

To declare a simple string, which is a scalar data type, $ should be used:

$str = "My name is Sam";
print "$str\n"

Here, the output would be the following:

"My name is Sam"

Perl also has many special variables, which are denoted by altering the normal variable indicators, as shown in this list.

An example of a more advanced program from Perl.org is below. This program sends an email using Perl script:

#!/usr/bin/perl
use strict;
use warnings;
# create the email message
use Email::MIME;
my $note = Email::MIME->create(
  header_str => [
    From    => '[email protected]',
    To      => '[email protected]',
    Subject => 'Happy New Year!',
  ],
  attributes => {
    encoding => 'quoted-printable',
    charset  => 'ISO-8859-1',
  },
  body_str => "Wishing you a very happy New Year!\n",
);
# send the email
use Email::Sender::Simple qw(sendmail);
sendmail($note);

Pros and cons of using Perl

Perl has several advantages, as well as some disadvantages.

Advantages

Perl's advantages include the following:

Disadvantages

Perl's major disadvantage is that it's a relatively messy language in a number of ways:

Perl vs. Python

Perl and Python have a shared history. Both were invented to simplify scripting. Perl was developed to give a C-like structure to Unix scripts. Python was invented to make C simpler and ready to script with.

Perl and Python are similar syntactically, and translating from Perl to Python is relatively easy with a few major syntax changes.

However, here are four key differences:

  1. Lines written in Perl end in a semicolon.
  2. Perl contains curly braces and indentation; Python does not.
  3. Variable names in Perl are styled using a variable indicator, like $x, %x and x. Variable names in Python are styled without a variable indicator, like x.
  4. The print statement in Python adds a new line at the end of output.

Other differences include the following:

History and future of Perl

Programmer Larry Wall created the first version of Perl in 1987. Perl was originally said to stand for "Practical Extraction and Reporting Language," but that name is no longer used. Wall prefers to use "Perl" for the language itself and "perl" for any interpreter or compiler of the language.

Perl 5 first became available in 1994. The latest stable version of Perl is 5.34.0, according to Perl.org.

Perl 6 is rooted in the same ancestor language as Perl 5 but is a separate programming language. It has its own design team of open source volunteers.

The Perl 6 project began after the 2000 Perl Conference, but the first official version of the language, version 6.c, wasn't available until December 2015. Perl 6 was renamed Raku in 2019.

Today, Perl refers to Perl 5. Work on Perl 7 began in 2020. It is expected to be available in 2021.

Learn more about Perl and four other programming languages that are still influential but are likely to be obsolete in the next decade.

16 Nov 2021

All Rights Reserved, Copyright 1999 - 2024, TechTarget | Read our Privacy Statement