| Previous | Table of Contents | Next |
This book is designed for everyonenovices and gurus alike. Well begin gently, with an introduction to some general programming constructs that youll find in nearly every computer language: if statements, while loops, and so forth. But by chapters end, youll be able to write useful Perl programs. Admittedly, theyll be pretty simple programs but, as youll see throughout the book, simple is usually all you need.
Some of you advanced readers are no doubt thinking to yourselves, Ive been programming C for eight years, and assembly language before that. I dont 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 youve programmed in, youll find something new in each chapter, even this one: The last two sessions discuss default variables and contexts, two features of Perl that you wont 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 variablesscalarsare 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!
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 (thats 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 doesnt 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 usefuland 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 programslots of them. Youll 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 Presss FTP site (ftp.waite.com).
Lets run our first Perl program. Following that great tradition of computer language books, well 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)
Dont forget the semicolon at the end! Every (well, nearly every) Perl statement ends in a semicolon. Thats how Perl can tell where one statement ends and the next begins, so if you leave a semicolon out, Perl will complain.
Now lets run the program. Type perl hello.
% perl hello RESULT: Hello world!
If that didnt work, then Perl isnt in your path, probably because it hasnt been installed. In that case, youll 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 #sliberallyto document your code so you [and others] can tell how your program works.)
On UNIX systems, youll 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 . isnt in your path. (If you dont understand that, dont worry about it.)
If hello didnt run, thats 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 youre not on a UNIX system. (MS-DOS users: You wont 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, youll 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 dont recognize the #! notation. Itll 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 |