next up previous contents
Next: Invocation Up: Writing Perl Programs Previous: Writing Perl Programs

Creating the Program

A Perl program consists of an ordinary text file containing a series of Perl statements. Statements are written in what looks like an amalgam of C, UNIX shell script, and English. In fact, that's pretty much what it is.

Perl code can be quite free-flowing. The broad syntactic rules governing where a statement starts and ends are:

Here's Our first uninspired Perl statement hello1.pl:

print("Hello World\n");

No prizes for guessing what happens when Perl runs this code-it prints out Hello World. If the "\n" doesn't look familiar, don't worry-it simply means that Perl should print a newline character after the text, or in other words, go to the start of the next line, exactly like C.

Printing more text is a matter of either stringing together statements like this, or giving multiple arguments to the print() function :

 print("Hello World,\n"); 
 print("I'm  alive\n");

So here's a small finished example hello2.pl, complete with the invocation line at the top and a few comments:

#!/usr/local/bin/perl -w 

 print("Hello World,\n"); 
 print("I'm  alive\n");

Unix has two ways of invoking a Perl program (see below) if you use (or intend to use) Perl on Unix always make this the

You can create your Perl program by starting any text processor:

Create a file called test.pl that contains the preceding three lines. In general convention all Perl files should end with a .pl extension. You can call your program anything you like, but it should be a meaningful description.


next up previous contents
Next: Invocation Up: Writing Perl Programs Previous: Writing Perl Programs
dave@cs.cf.ac.uk