Previous Table of Contents Next


CHAPTER 1
MEET PERL: THE BASICS

This book is designed for everyone—novices and gurus alike. We’ll begin gently, with an introduction to some general programming constructs that you’ll find in nearly every computer language: if statements, while loops, and so forth. But by chapter’s end, you’ll be able to write useful Perl programs. Admittedly, they’ll be pretty simple programs but, as you’ll see throughout the book, simple is usually all you need.

Some of you advanced readers are no doubt thinking to yourselves, “I’ve been programming C for eight years, and assembly language before that. I don’t need a book to tell me how to write a while loop or how to think about subroutines.” True. This book dispenses with the basics quickly, with thorough coverage of advanced topics such as networking, process management, and creating graphical applications from within Perl. No matter what other languages you’ve programmed in, you’ll find something new in each chapter, even this one: The last two sessions discuss default variables and contexts, two features of Perl that you won’t find in other programming languages.

This chapter, like all 14 in this book, is divided into eight sessions, each of which has a quiz and a programming exercise. After running a simple program in Session 1, simple variables—scalars—are introduced in Session 2, followed by the if and while statements in Sessions 3 and 4. Session 5 introduces arrays, which are collections of scalars. The for statement is introduced in Session 6, and then default variables and contexts are discussed in Sessions 7 and 8. Finally, there are four additional programming exercises at the end of this (and every) chapter.

Perl is fun, powerful, useful, and (at times) quirky. Have fun, and proceed at whatever pace suits you!

Session 1
The Look and Feel of Perl

Perl was born in 1987, when Larry Wall fused a system administration tool he had written with a UNIX utility called awk. Over time, new features were added: pattern matching, associative arrays, formats, a symbolic debugger, and oodles and oodles of functions. Now Perl is in its fifth incarnation (that’s “Perl 5” to you), supports object-oriented programming, and is used widely for systems administration, rapid prototyping, and the World Wide Web. Perl is one of the fastest-growing languages around. And getting started is easy.

Perl borrows features from a number of languages. C programmers will recognize many of the functions and operators in Perl, as well as the interface to the UNIX operating system. But unlike C, Perl doesn’t force you to compile binary executables that work on only one platform. In that respect, Perl resembles the UNIX shells, from which it borrows facilities for process management and file manipulation. Perl also supports sophisticated pattern matching, which makes tasks that would take hundreds of lines in C possible in one or two lines. These components make Perl useful—and eclectic. Larry Wall concedes that PERL might well stand for Pathologically Eclectic Rubbish Lister, although his original choice was Practical Extraction and Report Language.

This book illustrates Perl concepts with working programs—lots of them. You’ll learn a lot by typing them all in, but out of compassion, script for all the programs are available on the accompanying CD and at the Waite Group Press’s FTP site (ftp.waite.com).

Running a Perl Program

Let’s run our first Perl program. Following that great tradition of computer language books, we’ll begin with a program that prints Hello world!. Create a file (call it hello) containing the line shown in Listing 1-1.

Listing 1-1: A simple Perl program

print "Hello world! \n";      # prints Hello world! followed by
                                a newline (\n)

Don’t forget the semicolon at the end! Every (well, nearly every) Perl statement ends in a semicolon. That’s how Perl can tell where one statement ends and the next begins, so if you leave a semicolon out, Perl will complain.

Now let’s run the program. Type perl hello.

% perl hello
RESULT: Hello world!

If that didn’t work, then Perl isn’t in your path, probably because it hasn’t been installed. In that case, you’ll want to get it from one of the sources listed in Appendix A.

If you want hello to be executable directly from the UNIX command line so you can just say hello instead of perl hello, you need to add a line to the beginning of your program.

#!/usr/bin/perl
print "Hello world! \n";      # prints Hello world! followed by
                                a newline (\n)

(On all lines but the first, # is used to begin a comment. Use #s—liberally—to document your code so you [and others] can tell how your program works.)

On UNIX systems, you’ll need to make the program executable with the chmod command.

% chmod +x hello

Then you can run the program just by typing its name.

        % hello
RESULT: Hello world!

On a UNIX system, you may need to type./hello if . isn’t in your path. (If you don’t understand that, don’t worry about it.)

If hello didn’t run, that’s probably because your Perl is somewhere other than /usr/bin, where hello tried to find it. Perhaps your Perl is in /usr/local/bin, or perhaps you’re not on a UNIX system. (MS-DOS users: You won’t be able to use the #! notation unless you install a utility such as 4DOS or convert your Perl program into a batch file with pl2bat.)


#!/usr/bin/perl
This book will use #!/usr/bin/perl as the first line of all of its programs because Perl installs itself as /usr/bin/perl on most UNIX systems. If Perl is somewhere else on your computer, you’ll have to replace /usr/bin/perl with the appropriate pathname.

The instinfo program (available from ftp.waite.com) contains a trick for invoking Perl on computers that have UNIX shells but don’t recognize the #! notation. It’ll also print out where Perl was installed. For more information, consult the perlrun documentation bundled with the Perl distribution.

If you wish to change the initial line of the programs from #!/usr/bin/perl to, say, #!/usr/local/bin/perl, use the header program, also available from ftp.waite.com.


Previous Table of Contents Next