Lesson 5 | Perl Hello World! |
Objective | Run your first Perl script. |
Hello World! in Perl
Now that you have perl
properly installed on your system, it's time to write your first program and get it running.
The first program you write in a new language should always be extremely simple. The goal here is not to exercise the language, but rather to learn the exact steps necessary to edit, load, and run a program in this new environment. Because of that, it is traditional to write a program that displays the words
"Hello, World!" on the console. Here is that program in Perl:
#!/usr/bin/perl
print "Hello, World!\n";
Perl Language Overview
A discussion of the programming features and facilities of Perl is in order before we present the areas in which Perl can be applied. This will be an overview, so no attempt is made to provide an exhaustive or in-depth treatment. Components of Perl that are unique or unusual will be emphasized at the expense of features common to many languages.
Perl is an interpreted language, meaning that a control program that understands the semantics of the language and its components (the interpreter) executes program components individually as they are encountered in the control flow. Today this usually is done by first translating the source code into an intermediate representation (called bytecode) and then interpreting the bytecode. Interpreted execution makes Perl flexible, convenient, and fast for programming, with some penalty paid in execution speed.
Running Perl Program
Running Perl on a Unix system
- Step 1: Create Perl script
Copy the two-line Perl program below into a text editor and save it to the disk on your system as hello.pl
.
#!/usr/bin/perl
print "Hello, World!\n";
- Step 2:
The first line of the script, /usr/bin/perl
, should be replaced with the location of the perl binary on your system.
- Step 3:
Make sure that the program is executable. You do that by typing a command like this:
$ chmod 755 hello.pl
Use whatever name you have called your Perl program in that command. For example,
$ chmod 755 prog1.pl
- Step 4:Run the program
Now you should be able to run the program by typing the name of the file at the command prompt:
$ hello.pl
Hello, World!
Running Correct Path
If you are getting a command not found
error, it may be that ./ is not in your path.
Try running the program again by typing ./hello.pl
at the command prompt. If that does not solve it, check to make certain that the first line of the program contains the correct location of the perl binary on your system.
Running Perl on Windows
- Step 1: Create the script
Copy the two-line Perl program below into your favorite text editor, and save the file as hello.pl
.
#!/usr/bin/perl
print "Hello, World!\n";
- Step 2: Run Perl script
Launch MS-DOS from your Programs folder on your Taskbar.
At the command line, type:
perl hello.pl
- The program should run and print:
Hello, World!
Running Perl using DOS
Remember to type perl and then the path to your script at the command prompt each time you run a script.
If this program still does not work, you may want to consult the application documentation carefully. Chances are it's a problem with the installation.
Make sure that you understand the steps necessary to run the program on your system before continuing on to the next lesson.
Over the next several lessons, you will learn about the evolution of Perl as a language and the general structure of a Perl program.