Beginning Perl Lesson 2

Table of Contents

Creating and running a simple Perl script

It is traditional when learning a new programming language to create a first program named helloWorld. This first program simply prints the words Hello, world! to the screen and quits. Once you are able to accomplish this, you have learned all the prior steps required for writing and running programs in the new programming language.

Our script, which we will call helloWorld1.pl, is given below:

#!/usr/bin/perl

print( "Hello, world!\n" );

You can either copy this code from this page and paste your editor’s window, or type it in yourself. Save the file as helloWorld1.pl.

Then from the command line, move to the directory in which you placed the helloWorld1.pl file, and enter the command:

perl helloWorld.pl

This command starts perl, which then runs the script. If errors appear in your code, perl will tell you where the errors are and quit before running the script. If your script looks fine, perl will run it. The result should be that the words Hello, world! appear on the screen.

> perl helloWorld.pl
Hello, world!

The first line of the script is called the shebang line. The word shebang comes from the names of the first two characters of the line, the sharp (#) and the bang (!). These characters must be the first two characters of the file, and they are followed immediately by the pathname of the UNIX program that will run the script. In our example, we provide the pathname to Perl.

On a Macintosh computer running Mac OS X 10.3.4, the full path to Perl is /usr/bin/perl, so that is the path we’ll use in all the examles of this tutorial.

Starting a script with #! (shebang)

setting execute permission on a script

the difference between perl scriptname and ./scriptname

shebang line

For more information about the shebang line, read question 3.16 of the Unix Frequently Asked Questions.

Comments

In a Perl script, anything appearing after a # on a line is a comment. Comments are ignored by the Perl interpreter and thus provide a good way to provide information about a script. For every script you create, you should add comment lines that give:

  1. the name of the script
  2. the date the script was written
  3. the name of the author of the script
  4. contact information for the author
  5. a brief description of what the script does
  6. an example of how to run the script

Here’s our helloWorld.pl script with a full set of comments:

#!/usr/bin/perl
#
#   helloWorld2.pl
#   13-Jun-2004
#
#   Conrad Halling
#   conrad.halling@sphaerula.com
#
#   This script prints "Hello, world!" to the screen and then quits.
#
#   Use: perl helloWorld2.pl

    print( "Hello, world!\n" );

You should include these types of comments in all scripts you write, include your homework.

Homework assignment

Find or install an editor on your system that highlights the Perl code by its syntax.

Use your editor to write a Hello World type of script and run it. If you’re working on a Unix-like system (e.g., Unix, Linux, Mac OS X), change the mode of the script and run it as a system command.