Prev NEXT  


How Perl Works

Hello World

Once you have Perl loaded, make sure you have your path properly set to include the Perl executable. Then, open a text editor and create a text file. In the file, place the following line:

print "Hello World!\n";

Name the file "". At the command prompt, type:



Perl will run and execute the code in the text file. You should see the words "Hello World!" printed to stdout (standard out). As you can see, it is extremely easy to create and run programs in Perl. (If you are using UNIX, you can place a comment like #! /usr/bin/perl on the first line, and then you will not have to type the word "perl" at the command line.)

The print command prints things to stdout. The \n notation is a line feed. That would be more clear if you modified the test program to look like this (# denotes a comment):

# Print on two lines
   print "Hello\nWorld!\n";

Note that the print command understood that it should interpret the "\n" as a line feed and not as the literal characters. The interpretation occurred not because of the print command, but because of the use of double quotes (a practice called quoting in Perl). If you were to use single quotes instead, as in:

print 'Hello\nWorld!\n';

the \n character would not be interpreted but instead would be used literally.

There is also the backquote character: `. A pair of these imply that what is inside the quotes should be interpreted as an operating system command, and that command should be executed with the output of the command being printed. If you were to place inside the backquotes a command-line operation from the operating system, it would execute. For example, on Windows NT you can say:

print `cmd /c dir`;

to run the DIR command and see a list of files from the current directory.

You will also see the / character used for quoting regular expressions.

The print command understands commas as separators. For example:

print 'hello', "\n", 'world!';

However, you will also see a period:

print 'hello'. "\n". 'world!';

The period is actually a string concatenation operator.

There is also a printf operator for C folks.


In Windows NT, you cannot say:

print `dir`;

because dir is not a separate executable -- it's part of the command interpreter cmd. Type cmd /? at a DOS prompt for details.