Your First Program

 <  Day Day Up  >  

To write your Perl programs, you need a utility called a text editor . A text editor is a program that allows you to enter plain text, without any formatting, into a file. Microsoft Windows Notepad and the MS-DOS EDIT.EXE program are both examples of text editors. For Unix, vi, Emacs, and pico are all text editors. At least one of them will be available on your system.

Under the Mac, the MacPerl application contains a rudimentary text editor; to open a new program, select New under the File menu. For Mac OS X use the TextEdit program. This is located in the Finder under Applications. Be sure to set the preferences to "plain text" or select "Make Plain Text" under the Format menu.

You should not use a word processor to type in your Perl programs. Word processors ”such as Microsoft Word, WordPad, WordPerfect, and so on ”embed formatting codes by default when they save documents, even if the documents contain no boldface, italic, font changes, or other special formatting. These formatting codes will confuse Perl, and your programs will not work correctly. If you need to use a word processor, be sure to save your programs as plain text.

Typing Your First Program

Open your text editor, and type the following Perl program exactly as shown:

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

The #! line should be the first line in the file.

After you type this program into your text editor, save it in a file named hello . You don't need an extension on the filename, but Perl permits you to put one there. Some Windows and Macintosh utilities use extensions to tell what kind of file this is. If you need or want to use an extension, .pl or .plx is common; for example, you can use hello.pl .

Watch Out!

One of the most common problems here besets users of Microsoft Notepad. When you save the file, Notepad always appends a .txt extension to the file, whether or not you asked it to. If you want a .pl extension, you have to specify the filename in quotes, like this: "hello.pl" , as shown in the following figure. This forces Notepad to save the file without the .txt extension.


Running the Program

To run the program, for now, you need to get to a command prompt. In Unix, open a terminal window or log in. On a Microsoft Windows machine, open an MS-DOS prompt. Under Mac OS X, use the Finder, select Applications, Utilities and run Terminal. You should also change into the directory where you stored the hello program by using your shell's cd command.

When you're at the prompt, type the following. (A DOS prompt is shown here; Unix has a slightly different prompt.)

 C:\PROGRAMS>  perl hello  

If all goes well, Perl should respond with the following message:

 Hello, World! 

If typing this command worked, congratulations! Remember how you ran this program because this is how you will start your programs for the rest of the book. (You can use some other ways, which will be covered shortly.)

If it did not work, check for the following:

  • If you receive the error message Bad command or file name or perl: command not found , the Perl program is not in your execution path . You need to figure out where the Perl program is installed and add that directory to the PATH variable in your shell.

  • If you receive the error message Can't open perl script hello: A file or directory does not exist , you're probably not in the same directory as the file named hello that you saved earlier, or you saved the file under a different name. To change into the proper directory under Unix or Windows, use the cd command. For example, if you saved the file on your desktop in Windows 98, the command would be:

     c:\> cd \Windows\Desktop c:\> perl hello 

    Remember, if you used an extension on the filename (such as .pl or .plx ), you need to specify that extension when you run the program.

  • If you receive an error such as syntax error , Perl starts normally but cannot figure out what is in the file named hello . You've either mistyped the file's contents, or you've used a word processor that applied formatting to the saved file. The cat command (Unix) or the type command (DOS) can be used to verify what is in the file. If you mistyped, check everything; the quotation marks and punctuation are important.

If you're using MacPerl, simply select Run "hello" from the Script menu to run your first Perl program. If you're not using MacPerl's built-in editor to write your program, use the Open command from the File menu to open your program in MacPerl, and then select Run.

It Worked! So What Happened?

When you typed the command perl hello , a program named perl was started on your computer. This program is called the Perl interpreter . The Perl interpreter is the heart and soul of Perl. Its job is to take the file it has been given ( hello , in this case), find the program inside it, and run it.

When it runs a program, the interpreter "first checks the statements, functions, operators, math, and everything else that makes up the Perl program to make sure the syntax is correct, and then executes the statements one at a time."

When the Perl interpreter is all done reading your program from disk, it begins to run the program and continues running that program until it's all done. When it's finished with your program, the Perl interpreter exits and returns control to your operating system.

Now let's look at how the hello program is "run."

Perl Play-by-Play

The first line of the hello program is

 #!/usr/bin/perl 

To Perl, anything following a # on a line is considered a comment line. A comment is something that Perl ignores. The #! on the first line of a program is different in some cases. The pathname that follows ” /usr/bin/perl ”is the path to the Perl interpreter. If a Unix file begins with #! followed by the path of an interpreter and is executable, Unix knows that this is a program (rather than a shell script) and how to interpret it. See the "Q&A" section at the end of the hour for an explanation of how to run programs.

Some Web servers that can execute Perl programs ”Apache, for example ”also pay attention to the #! line and can call the programs without having to use an explicit perl command.

For now, just consider #! a comment line.

The next line of the program is

 print "Hello, World!\n"; 

A whole lot is going on here. This line constitutes a Perl statement; it marks off for Perl one thing to do.

First, this line contains a function called print . The print function takes whatever follows it and displays it to your screen by default. What the print function is supposed to print lasts all the way up to the semicolon ( ; ).

The semicolon in Perl is a statement separator . You should put a semicolon between statements in your Perl program to show where one statement ends and the next one begins.

In this case, the print function displays the phrase Hello, World! . The \n at the end of the line tells Perl to insert a new blank line after it prints the phrase. The quotation marks around the phrase and the \n tell Perl that this is a literal string, and not another function. Strings are discussed at great length next hour.

Something You Should Know

Perl is called a free-form programming language. This means that Perl statements aren't very picky about how they're written. You can insert spaces, tabs, and even carriage returns ” called whitespace ”nearly anywhere in a Perl statement, and it really doesn't matter.

The only places that you really can't insert whitespace willy-nilly are places you'd expect restrictions to be. For example, you can't insert spaces in the middle of a function name; pr int is not a valid function. Nor can you insert them in numbers , as in 25 61 . Also, whitespace inside literal strings like "Hello, World!" shows up as, well, whitespace. Almost everywhere else it's valid. You could write the sample Perl program like this:

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

It would have functioned identically to the original. This free-form nature gives you a lot of range in "style" in your Perl programs. Feel free to express yourself with form. Just remember, someday other users may have to look at your programs. Be kind to them.

The style used in the examples in this book is fairly conservative. Sometimes statements are broken across several lines for clarity or to save space because Perl statements can get quite long. The Perl documentation even has a style guide, if you want to browse it for suggestions. You should search for the document named perlstyle.

By the Way

Style in Perl programs can be taken to extremes. Valid Perl programs can be written as poetry, even haiku. Some memorable Perl programs actually look like pictures but do something useful. The Perl Journal (http://www.tpj.com) used to run a contest that was all about writing obscure-looking Perl programs: the Obfuscated Perl Contest. You should not take style lessons from these entries.


 <  Day Day Up  >  


SAMS Teach Yourself Perl in 24 Hours
Sams Teach Yourself Perl in 24 Hours (3rd Edition)
ISBN: 0672327937
EAN: 2147483647
Year: 2005
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net