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";
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.
Hello World! on Mac
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";
Saving .pl file
You can save the
.pl
file anywhere.
However, you may want to create a special working folder for this course in which you will save all of your scripts.
- Step 2 - Run the script
Open MacPerl. Select Run Script from the Script menu.
From there, you will be prompted to select a file. Navigate to hello.pl
and click Open.
The program should run and print:
Hello, World!
Perl Installation
If this program is not working, you may want to consult the application documentation carefully before posting a message to the forum.
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.
