Hello, world, Perl style

Introduction

Way back in the stone age (er, the 1970's) when Kernighan & Ritchie first created the C programming language, they introduced it to the world in the famous white book with their "Hello, world!" example.

After all these years, it still seems like a good way to introduce a programming language, so here's our version of "Hello, world!", written in Perl:

#!/usr/local/bin/perl 
print "Hello, world!\n"; 

Listing 1 (above): The source code for the "Hello, world!" program.

Discussion

The first line of the program specifies the location of the Perl interpreter. On Unix servers, the Perl program is usually installed in the /usr/local/bin directory. This special syntax is the proper way of indicating to a Unix operating system that it should use this interpreter to read the next lines of code.

The second line of the program is a simple Perl print statement that prints out the now-famous phrase. When you run this program from the command-line, the words "Hello, world!" are printed to your screen (followed by a carriage-return).

That's the whole program for this simple example. Just save these two lines of code to a file named hello. Then, to run the program on a Unix system, you'll need to make the program executable. You can do this with the chmod command:

chmod +x hello

Once the file is made executable, you can run it by entering the name of the file, like this:

hello 

Note: If your current directory isn't in your path, you'll need to (a) change your PATH variable, or (b) run the program like this:

./hello 

Final thoughts

We hope this simple Perl program leads you along the road of learning a terrific programming language. With Perl you'll be able to accomplish great things with a little bit of code in a short time!

What's Related

We also have our own devdaily.com Perl FAQ list, which keeps growing every year.


devdaily logo