GNU/Linux Programmer's Solver - Perl

Hello World program in Perl. Basic structure, comments and string literals
PROGRAMMING PERL HELLO WORLD
#!/usr/bin/perl

# This is a comment
=pod
This is a multiline
comment in Perl
=cut
print "Hello World!\n";

print <<EOM
----------------------------
This is a multiline
text message in
Perl.
----------------------------
EOM

Perl is an interpreted language, which means that the source code (the program we write) can be executed directly by a language interpreter.

Perl ends instructions with the character ; and allows grouping instructions using curly braces ({ }), similar to the C language. Constant character strings are also represented between double quotes ".

Programs in Perl usually use the .pl extension.

Perl uses the print function to print messages to the screen. As we can see, the parameters to the print function can be passed directly; it is not necessary to enclose the list of parameters in parentheses or other characters. As we will see later, parentheses for functions in Perl are actually optional as long as it is clear that we are calling a function.

Perl supports single-line comments using the # character, but does not allow multi-line comments. In case they are needed, and if our text editor does not have a function for commenting blocks, we can use POD (Plain Old Documentation). This is a Perl code documentation system (which is what comments are for). It’s actually a complete markup language that can be used to generate documentation for our program, but it can also be used to add multi-line comments.

The paragraph commands (as they call them) =pod and =cut allow us to define a POD block that we can use to add multi-line comments, as we can see in the example code.

DID YOU KNOW…

The Perl programming language is written “Perl”. The word “perl” in lowercase refers to the interpreter program, and PERL in uppercase is used ironically with acronyms such as: Practical Extraction Report Language or Pathologically Eclectic Rubbish Lister.

Finally, as we can see, Perl allows us to define here-docs. I’m not sure how best to translate this. As you can see at the end of the program, they allow us to write long texts, ending them with a marker of our choice. This is tremendously useful, especially when used with variable expansion, and they were extensively used in the early days of the web in CGIs (Common Gateway Interface), one of the first web technologies for executing code on the server side.

To run the program, it is necessary to give the file execution permissions. If we have added the “she-bang” line (#! at the beginning of the file), we can execute the program directly, otherwise we will need to pass it as a parameter to the Perl interpreter (which is basically what #! does).

    $ perl hello.pl
    $ chmod +x hello.pl
    $ ./hello.pl

Perl was developed by Larry Wall in 1987 while working for Unisys, as a Unix script language to facilitate report processing. Perl evolved to version 5, and in the year 2000 it underwent a redesign that resulted in a distinct language known as Perl 6.

Initially, system manual pages were the only documentation of the language (which, all things considered, are fabulous), until the book Programming Perl, also known as The Camel Book, one of the legendary books in hacker culture, was published in 1991.

DID YOU KNOW…

Besides the Camel Book, there’s the Llama Book, originally titled Learning Perl. This book, written in tutorial form, is an excellent resource for getting started with the language.

Perl 5 was released in 1994 and is the most widespread version of the language. Part of this popularity was due to the large number of modules created for this language, making it possible to do practically anything. This culminated in the establishment of a general module repository in 1995 known as CPAN or Comprehensive Perl Archive Network. We can find an immense amount of modules in this repository.

The latest stable version of Perl is 5.34, released in 2021. Besides version 5 (which we will use here), there are versions 6 and 7. Perl 6 was renamed Raku, which is not compatible with Perl 5 (it’s a different language) and was developed as part of the Parrot virtual machine specification as part of the language update process that took place in 2000 through the well-known Apocalypses (documents describing the planned changes to the language).

Perl 7 was announced in 2020 and will be the next version of Perl 5, with which it will be compatible.

Summary

  • Interpreted language
  • Extension: pl
  • Line terminator: ;
  • Groups instructions with: {...}
  • Multi-line comments: =pod/=cut
  • Single-line comments: #
  • String delimiter: "string" or 'string'
  • You can print text with print (does not add carriage return)
  • Does not support multi-line strings
  • Supports HERE-DOCS
  • Entry point: Beginning of the file

Related Posts

GNU/Linux Programmer's Solver
Chapter 1 - Hello World

Hello World is the first program you write when learning a new language and there are good reasons for that

Read

PROGRAMMING HELLO WORLD

GNU/Linux Programmer's Solver - Objective-C Language

Hello World program in Objective-C. Basic structure, comments and string literals

Read

PROGRAMMING OBJECTIVE-C HELLO WORLD

GNU/Linux Programmer's Solver - Python

Hello World program in Python. Basic structure, comments and string literals

Read

PROGRAMMING PYTHON HELLO WORLD

Return to Home Page