Hello Perl

The first step in learning to program is to teach the computer to say hello. With Perl, this is incredibly straightforward: you simply tell perl to execute the following code:

  say "Hello World!"

The obvious question is: "How do I 'tell perl' to execute this code?" Depending on your operating system, you might need to download and install the 'perl' interpreter — a program which executes Perl code. (Note: uppercase "Perl" refers to the programming language while the lowercase "perl" means the interpreter itself.)

First, you should see if you already have perl installed and what version it is.

You'll need to open a command-line terminal. On OS X, use the finder to launch Applications -> Utilities -> Terminal. On Ubuntu, this is Applications -> Accessories -> Terminal. On Windows, click Start -> Run, type cmd into the prompt, and click OK.

At the command-line, type perl --version, hit Enter, and see if you get some output with a version number. If you get something like 'perl' is not recognized... or perl: command not found, or your version is less than 5.10.0, then you'll need to download and install perl.

With perl 5.10 installed, you may now rush headlong into programming by running:

  perl -E "say 'Hello World!'"

If your computer is a Mac or an older Linux distribution, you might already have perl 5.8.8 installed. This is a fine version of perl, but lacking some nice features which are only available in 5.10, starting with the say function and the -E option!

Before 5.10, this sort of thing would have been done with -e and print:

 perl -e 'print "Hello World!\n"' 

This will still work in perl 5.10 (try it!) because new versions of Perl 5 never remove features. It also introduces the newline character and an important detail about quoting strings.

Now run the same command without the \n and see what happens.

 perl -e 'print "Hello World!"' 

The output gets crowded by the new prompt because it has no final newline. (Tip: Hit Ctrl+C to get a fresh, uncrowded prompt in your terminal.)

This is called string interpolation, where double-quoted strings do nifty things like translating backslashed characters into some special kind of output.

Another fun example (until your coworkers get upset) is print "\a" — the \a is the "bell" character (it makes your terminal beep.)

Notice that the say command in 5.10 is the same as print except it includes the newline for you (not to mention being slightly shorter and more conversational.) The capital -E (instead of -e) tells perl that you intend to use say and other 5.10-only features in your program.

Aside: Windows users will need to write this slightly differently due to the way in which the Windows cmd shell deals with quoting. In Windows, the command-line does not treat the single quote (') as a quote, so everything must be in double quotes ("). But, Perl has a way around this by using the qq{...} operator.

  perl -e "print qq{Hello World\n}"

Your First Perl Program

Once you have run these examples, you have actually just written your first program in Perl. The -E switch allows you to write complete programs directly at the command-line, but hassles with quoting can get in the way (as we saw earlier) and it is difficult to refer back to this "program" tomorrow. For longer-lived programs, it is much more convenient to save your code in a file.

To save a Perl script, you create a plain text file containing Perl code. You'll want to start a text editor now. (Note that a "word processor" is not what you want.)

If you're running Windows, click Start -> Programs -> Accessories -> Notepad. OS X users can use Applications -> TextEdit, but remember to click the menu Format -> Make Plain Text. On Linux, look for gedit or kwrite in your launcher menu.

Now type the following program into your editor and save it as hello.pl.

  #!/usr/bin/env perl

  use v5.10.0;
  use warnings;
  use strict;

  say "Hello World!";

Once you have saved the program, tell perl to run it:

  perl hello.pl

Now we're running the perl interpreter by telling it the name of the program to run. We also have three new 'use' statements which you'll include in all of your programs. The first, use v5.10.0 tells Perl that you want all of the features of 5.10.0 (and notifies an earlier perl such as 5.8.8 that it is not modern enough to understand this code.) The use strict and use warnings statements help catch typos and other simple programming errors. By starting all of your Perl programs this way, you let perl make your life easier.


Notice that our program starts with what is called the "shebang" line. This is a special kind of comment on the first line which tells the operating system that this is a Perl program (except on windows.) By including the shebang line and setting the executable bit (chmod +x hello.pl), you turn the plaintext file into an executable program. Now you don't need to include 'perl' on the command line to run the program (but you do need to use ./ to reference it unless this program is in a directory in your $PATH.

Congratulations! You've just created and executed your first Perl program. You also learned how to write short programs directly on the command line, a bit about your command-line shell, and quoted strings. Now you have everything you need to begin programming in Perl.