Getting Started with Perl

Once Perl is installed, it is possible to start writing Perl scripts. Perl can be written in a simple text editor, such as Notepad, however, an Integrated Development Environment, or IDE for short, that supports Perl, will offer additional features such as syntax highlighting and maybe even code completion.

Code Commenting

When programming in any language it is useful to add comments to the code to describe what is happening and why. This is useful when the code is revisited at a later date to make enhancements, or for debugging purposes. In order to add a comment in Perl it needs to start with ‘#’, as shown below.

# This is a comment in Perl.

First Perl Script

The Perl code written below simply displays the words ‘Hello World!’ in a console or terminal window.

print "Hello World!";

Note that the word ‘print’ in blue above is known as a reserved word. Reserved words in a programming language have a specific purpose and therefore cannot be used for anything else. In this case ‘print’ displays a message in the console or terminal. If the above code is saved into a file with a ‘.pl’ extension, for example, ‘hello.pl’, it can then be run.

Running a Perl Script

Running a Perl script can be done from the command line as follows.

perl hello.pl

This assumes that the user is in the directory where the script resides, otherwise the full path would have to be provided before the file name.

Making Perl Scripts More Robust

By default Perl is a very forgiving language. In order to make it more robust it is a good idea to add the following two lines at the top of scripts.

use strict;
use warnings;

The first line above will catch potential problems and immediately stop the script from running. In contrast the second line will just provide a warning and allow the code to continue running.