Another Example: Create Hello World As a Script

Now let's do the same thing, only this time we'll create Hello World as a separate file and execute the script that way. You'll need a text editor. Not a word processor, but specifically an editor just for text: emacs, vi, or pico are just fine for Unix; use Notepad or the shareware programs TextEdit or UltraEdit on Windows; you can use SimpleText BBedit or any of the mentioned Unix tools on Mac OS X.

Create Hello World as a Script

Fire up your text editor and type the two lines shown in Listing 1.1 (well, three if you count the blank one in the middle).

Listing 1.1. The hello.pl Script
1: #!/usr/bin/perl -w
2:
3: print "Hello, World!
";

Note

Don't type the numbers at the beginning of the lines in the listings throughout this book; they're just there so I can tell you, line by line, what's going on in the script.

Also note that if you are in Windows that this version of Hello World is slightly different from the one-liner version, so don't just repeat what you did there.


If you're on Unix, you'll want to make sure you include that first line (colloquially called the “sh'bang”—or shebang—line: “sh'” for the hash (or sharp) and “bang” for the exclamation point). This line tells Unix which program to use to actually run the script. The one gotcha here is that the pathname in that line has to match the actual path to the Perl interpreter; if you installed it in /usr/bin/perl or somewhere else in your system, include that pathname there instead of the one in line 1 of Listing 1.1.

If you're on Windows or on the Mac, you don't usually need to include this first line at all. It won't really matter either way because the # at the start of the line is a comment, and Perl will ignore that line on platforms other than Unix. However, if you intend to write Perl scripts that will eventually be run on Unix, it is a good idea to get into the habit of including the shebang line, even if it's not entirely necessary.

Note

If you're intending to use Perl on Windows for Web CGI scripting, some Web servers (notably Apache) require a shebang line in your Perl scripts (albeit one with a Windows-style path starting with C:). Yet another reason to get into the habit.


Save that file as, say, hello.pl. Actually, you can call it anything you want, with or without the .pl extension. If you're on Windows, you'll probably want to include the .pl, though; Windows prefers its programs to have extensions, and it'll make things work better overall.

Note

Don't feel like typing? All the scripts you'll be exploring in this book are also contained on the Web site for this book at http://www.typerl.com/, so you can use the versions there instead of laboriously typing them in. However, typing in a Perl script (and fixing the errors that inevitably occur) helps you learn how Perl scripts work, so you should try typing at least these first couple scripts.


Running the Hello World Script

The next step is to use Perl to actually run your script.

On Unix, you'll need to make your script executable, and then simply type the name of the script on the command line like this:

% chmod +x hello.pl
% hello.pl
						

Note

Depending on how your execution path is set up, you might have to include the current directory when you call the script, like this:

% ./hello.pl
							


On Windows, open a DOS or command window and type perl -w and the name of the script, as shown here:

C:perl> perl
							-w hello.pl
						

If you're on Windows NT and have Perl set up so that .pl files are associated with Perl scripts (as described in Appendix B), then you should be able to just type the name of the script itself, as follows:

C:perl> hello.pl
						

Note

If you're on Windows 9x, you'll need to use the full perl command each time.


On any platform, you should see the phrase "Hello, World!" printed to the screen.

What to Do if the Hello World Script Doesn't Work

If you don't get “Hello, World!” printed to your screen this time, run through all the same checks you did with the one-liner to make sure that you've typed the right thing with all the right quotes. Make sure the first line starts with a hash mark (#).

If you get a File not found error or Can't open perl script, make sure you're in the same directory as your Perl script, and that you're typing the filename exactly as you did when you saved the script.

If you get a Command not found error on Unix, make sure the path in your shebang line matches the actual path to your Perl interpreter.

If you get Permission denied on Unix, make sure that you've remembered to make your script executable (use the chmod +x command).

How Does It Work?

So, now you've got a one-line Perl script that prints the phrase "Hello, World!" to the screen and a two-line file that does the same thing. It seems simple enough, but there's some basic Perlness about that script that I should describe.

First of all, although this is one of the simplest examples of a Perl script, the idea for larger Perl scripts is the same. A Perl script is a series of statements, executed one at a time starting from the top and continuing to the bottom. (There are occasionally digressions to subroutines, bits of code executed multiple times for loops, or code from programs included from separate libraries and modules, but that's the basic idea).

The first line in the Hello World script is a comment. You use comments to describe bits of Perl code to explain what they do, as well as to add reminders for things you have yet to do—basically, to annotate your script for any particular reason you'd like. Comments are ignored by Perl, so they're there exclusively for you and anyone else who might be reading your code. Adding comments to a script is considered good programming style, although generally you'll use fewer comments than actual code in your script.

This particular comment in Listing 1.1 is a special kind of comment on Unix systems, but it's a comment none the less. Perl comments start with a hash mark (#), and everything from the hash until the end of the line is ignored. Perl doesn't have multiline comments; if you want to use multiple lines you'll have to start them all with a hash.

Note

Actually, Perl does have multiline comments, but those are used mostly for included Perl documentation (called PODs), and not for actual comments. Stick to hashes.


The second line of in the file (line 3 in Listing 1.1), and the only line in the one-liner, is an example of a basic Perl statement. It's a call to the built-in function print, which simply prints the phrase “Hello, World!" to the screen (well, actually, to the standard output, but that's the screen in this case. More about the standard output tomorrow). The inside the quotes prints a newline character; that is, it shifts the cursor from the end of the current line to the start of the next line. Without it your script will end with the cursor still at the end of the Hello World phrase and not neatly on the next line.

Note also the semicolon at the end of the line. Most simple Perl statements end with a semicolon, so that's important—don't forget it.

You'll learn more about all these concepts—statements, comments, functions, output, and so on, later on in this chapter and tomorrow in Day 2, “Working with Strings and Numbers (Scalar Data).”

A Note About Warnings

Turning on warnings is an extremely good idea when you're learning to write Perl scripts (and often a good idea even when you're experienced at it). Perl is very forgiving of strange and sometimes wrong code, which can mean hours spent searching for bugs and weird results in your scripts. Turning on warnings helps uncover common mistakes and strange places in your code where you might have made mistakes. Get in the habit of it and it'll save you a lot of debugging time in the long run.

There are various ways of turning on Perl warnings depending on whether you are using Perl one-liners or scripts, and sometimes depending on the platform you're running on.

  • For any Perl one-liner, use -w on the command line.

  • On Unix, use the -w option in the shebang line.

  • On Windows, call your Perl scripts using the perl command and use -w on the command line, or, if .pl files are associated with Perl, use the -w option in the shebang line.

  • In MacPerl, choose “Compiler Warnings” from the Script menu.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset