HAPPY BOOKSGIVING
Use code BOOKSGIVING during checkout to save 40%-55% on books and eBooks. Shop now.
Register your product to gain access to bonus material or receive a coupon.
Supercharged Perl programming for every developer, sysadmin, and Web professional.
Modern Perl Programming will help you master Perl's most robust, powerful featuresso you can make the most of the language whether you're a beginner or an experienced scripter. Michael Saltzman starts with a rapid-fire Perl tutorial and refresher. Then, using actual applicationsnot just code snippetshe presents expert guidance on the Perl features that deliver the most power, productivity, and efficiency.
Coverage includes:
Includes extensive downloadable library of code samples! You won't just get results fast: You'll get great results fastrobust, maintainable, industrial-strength Perl for even the most challenging environments.
The EXAMPLES and SOLUTIONS directories support the Prentice Hall book MODERN PERL PROGRAMMING by Michael Saltzman.
Each directory contains 16 sub directories, 1, 2, 3.... 16 which represent the chapters in the book.
EXAMPLES\1 contains coded examples in the 1st chapter of the book.
EXAMPLES\2 contains coded examples in the 2nd chapter of the book.
etc.
SOLUTIONS\1 contains the solutions to the labs for chapter 1.
SOLUTIONS\2 contains the solutions to the labs for chapter 2.
etc.
The file labfiles.zip contains the EXAMPLES and SOLUTIONS directories in a zip archive.
What Is Perl?
Why program in Perl?
Why did I write this book?
Who should read this book?
Overview of the Book.
Chapter Summaries
1. A Tutorial Introduction to Perl.
Getting Started. Perl Variables. Control Flow Constructs. Altering Loop Control Flow. A Few Special Perl Variables. Regular Expressions. Writing Your Own Functions. Exercises.
Filehandles. Creating Your Own Filehandles. File Operators. The stat Function. Other Filehandles. Exercises.
Perl Operators. Exercises.
Introduction. The @ARGV Array. Array Functions. Exercises.
Introduction. Sorting Hashes. Hash Solutions to Common Problems. Built-in Associative Arrays. Reading from a File into a Hash. Exercises.
Introduction. The Match Operator. Regular Expression Metacharacters. Anchoring Patterns. Remembering Parts of Matches. Greedy Regular Expressions. Substitutions. Backtracking. The tr Operator. Exercises.
Introduction. Passing Arguments to Subroutines. Returning Values from Subroutines. Perl Libraries. The Perl Standard Library. Packages. Predefined Subroutines. Exercises.
Introduction. Format. Top-of-page formatting. The select Function. Bottom-of-Page Formatting. Exercises.
Introduction. File and Directory System Calls. Process Creation. Updating Files. Exercises.
Introduction. Anonymous References. Complex Data Structures. Collections of Records. Exercises.
Introduction. The Vocabulary of Object Orientation. Defining and Using Objects. Inheritance. Exercises.
Introduction. Writing Clients. Writing Servers. Iterative Servers. Exercises.
Introduction. HTML and Forms. Relationship between Server and CGI Script. cgi-lib.pl and cgi.pm. CGI Scripts and Databases. Exercises.
Introduction. Geometry Management. Buttons. Other Widgets. Frames. Binding. Exercise.
Introduction. A Review of SQL and Relational Databases. Accessing a Database from Perl. Adding a Graphical Front-End. Accessing a Real Database via a Web Form.
Introduction. Using the print Function. Command-Line Options. Executing Portions of Your Code. Quoting. Useful Perl Modules. The Perl Debugger. Useful Perl Functions.
Welcome to the world of Perl programming. I know that few of you enjoy reading the preface, but it may help to at least browse through it to make the reading of the actual text a little easier. In this section I will tell you a little about Perl and a lot about conventions used in this book. But first, I have a few questions to answer.
Perl is a programming language that was created by Larry Wall in the early 1980s. The language has gained widespread popularity over the last few decades. In the beginning, Perl was used by a small group of Unix system administrators who needed a language, superior to the Unix shell, to do various system administration tasks. With the exponential growth of the Internet and the popularity of the World Wide Web's Common Gateway Interface (CGI) programming paradigm, the number of Perl programmers has also grown exponentially.
Being familiar with many programming languages, I'm in a good position to judge them. I have had my favorite languages throughout the years: Fortran, PL/1, C, C++, Java, and of course, Perl. Too many people argue about which language is the best. No language can be all things to all people. Each language has its strengths and weaknesses. However, no language that I know has the expressive capability and ease of use of Perl. You, of course, can decide for yourself when you finish reading this book.
I have been an instructor of computer programming languages for more than three decades. In that time, I have had the occasion to learn many languages. My typical approach would be to buy a book on the subject, plow through it, and write as many programs as I could to test out various features of the language.
I'm very critical of textbooks. Most of the textbooks that I've read never seem to be at the correct level. Half of them seemed too elementary; the other half seemed too advanced. I always wanted one that was right in the middle. I've tried to write this book for the intermediate audience because I think I can save people in that category a lot of time and research.
This is a book about Perl programming, so anybody who is interested in writing Perl programs should read this book. Since Perl is a programming language, you would think this means only programmers should read it. Currently, however, there is a blur over who is a programmer. Many Web-sters who maintain Web pages also write CGI scripts. System administrators need to write scripts to support users. Managers periodically want to write scripts to create reports for various staff members above and below them in the corporate hierarchy. All these groups would profit from reading this book.
The book is organized in three sections: beginning, intermediate, and advanced. In the beginning section, you will see topics such as: input and output, operators, arrays, control structures, and data types. In the intermediate section, you will see topics such as: associative arrays, regular expressions, subroutines, report writing, and accessing system resources. The advanced section includes topics such as: references, object-oriented programming, client/server programming, CGI programming, graphical user interfaces (GUIs) with the Tk module, accessing real databases in Perl, and debugging.
demonstrates many fundamental Perl features so that you can start writing Perl programs quickly. Subsequent chapters will drill more deeply into each of the topics presented here.
looks more deeply into reading and writing files. Some of Perl's operators for determining the type of a file are explored. Finally, this chapter looks at how you can determine information about files.
examines the Perl operators. A Perl expression is a syntactically correct combination of operators and operands. Perl has a plethora of operators. Many of them originated with the C language and some of them are particular to Perl. In this chapter, we will explore all of the operators, including those used for bit manipulation, string manipulation, and regular expressions.
takes an in-depth look at arrays in Perl. All programming languages use arrays heavily and Perl is no exception. However, you will see that Perl arrays are dynamic and are supported by a wide variety of functions. We will take a look at some of the nice features of Perl arrays. Finally, we will look at many of the more common array functions used by Perl programmers.
looks at a common scenario in programming languages: paired data. Associative arrays, also called hashes, provide an efficient way to search for values by their keys. In this chapter, we will show how hashes may be used to efficiently solve a wide variety of commonly encountered problems.
refers to patterns that are expressed with characters armed with special meanings. These patterns may be compared to strings to see if the strings match the patterns. These regular expressions make it easy for Perl to validate data, make substitutions, or pick out portions of a match.
investigates the wide gamut of possibilities when you write your own functions. This chapter examines all of the principles having to do with subroutines in Perl. Some languages refer to these blocks as functions, while others refer to them as subroutines, and still others refer to them as procedures. In Perl, they are referred to as either subroutines or functions. Perl does not differentiate between these two terms.
demonstrates Perl's report writing capabilities. Reports are typically written by system administrators to summarize user and file activity. You can have Perl automatically generate headers and body formats. This chapter gives the details of the rudimentary report writing capabilities of Perl.
reveals the many system calls that are available via Perl. There is a strong Unix flavor to these functions. However, many of them have been implemented on Windows so that they have a consistent behavior regardless of the operating system.
moves the reader along into slightly advanced concepts. This chapter concentrates on a thorough treatment of references, Perl's version of pointers. References are used for almost all advanced Perl topics.
gives the details on how to write Perl programs that use objects and methods. This important chapter is the basis of many of the Perl modules found at various Perl repositories on the Internet.
explores the possibilities of writing network applications with Perl. After reviewing some networking fundamentals, we'll give some examples of writing clients that connect to real servers. Then we will write a few servers of our own.
explores the topics of writing back-end programs invoked by Web servers to process front-end fill-in forms. This style of programming, called CGI scripting, is largely responsible for the exponential growth of Perl.
gives a great many details about writing GUIs in Perl using the Tk.pm module. This is a great undertaking as this is a vast body of knowledge. This chapter demonstrates many Tk capabilities, but at a high level. All the methods in this section take many options. We will only show a few for each and leave the rest for a book that specializes in writing GUIs in Perl using the Tk module. Our approach in this section will be to introduce a few widgets and build a program in pieces. At the conclusion of this section, we will have built a GUI with an application behind it.
shows how to integrate SQL commands within your Perl scripts so that real databases may be accessed. To remain vendor-neutral, we will use the MySQL database, which can be downloaded at www.mysql.com
. With this database, we will demonstrate the principles of accessing all databases in Perl, regardless of whether the actual database is Oracle, Sybase, Access, etc.
explores many of the possibilities of debugging your Perl code. Debugging a piece of software is a difficult task in any programming language. Since Perl is so permissive, there is much room for error. This chapter shows many techniques for finding errors, including the use of the Perl debugger itself.
Perl programs run in many environments, though the two major operating systems in the marketplace of 2002 are Unix and Windows (and their variants). As you will see, there is a slight difference between executing a Perl program in these two environments. To normalize the way a Perl program is executed in this book, we will always use the % symbol as the system prompt. If you are using Unix, the first line of every script needs to be:
#!/usr/bin/perl
where the first two characters are #! and the rest of the line is the complete pathname for the Perl executable. We used /usr/bin/perl, where it is located on Linux, but it may be different on your system.
We will simply give the name of a Perl program to execute it, although on Windows, you'll need to put the word "perl" in front of the executable file. On Unix, you will want to make each file executable by using the chmod command. We will not show the chmod command being executed each time. We will simply show the execution of Perl scripts as follows:
% countfiles.plThere are 10 files in the current directory%
In the Perl world, there is no restriction on the naming of Perl executable files. Despite this lack of restriction, we will always name our example scripts with the .pl extension.
Perl matured on Unix. On those systems, there is a common notion of two popular files-the standard input file (the keyboard) and the standard output file (the display). This nomenclature has been adopted by the Windows world as well. Thus, we will often speak of a program that reads from the standard input, and we will also speak of a program that writes to the standard output. The following program is one of them:
% addinput.plEnter first number 25Enter second number 50Sum is 75%
The < symbol may be used to redirect the standard input so that the data can come from a disk file. The > symbol may be used to redirect the standard output to a disk file. We will use these notations whenever necessary. For example, the following demonstrates how to read from a disk file and write to another disk file:
% printlines.pl < inputfile > outputfile
Because of the < symbol, the program takes its input from the file named inputfile
rather than from the keyboard. Likewise, the program directs its output to the disk file named outputfile
rather than to the display.
If you are a Unix user, you are familiar with the newline character. It is generated when the user presses the Return (or Enter) key. Perl scripts honor this convention regardless of the operating system.
In writing a book about a programming language, the author is faced with many different decisions. Since this book contains many complete code examples, I must show you the code and explain it. This leads to two inevitable choices: show the code and then write about it, or write about it and then show the code. There are drawbacks and advantages to each choice. For the most part, I have given the code first and then given the explanation. When you become frustrated reading some of the code, have a little patience, for the explanation will surely follow.
Occasionally, you may need to display a few data files so you can interpret the results of programs. On Unix systems, this is usually accomplished with the cat command; on Windows, this is usually accomplished with the type command. To stay neutral, we will use a mythical display command as follows:
% display datafileMichael Saltzman Columbia MarylandSusan Saltzman Charleston South CarolinaErin Flanagan Annapolis MarylandPatti Ordonez Columbia MarylandMaria Gonzalez Pikesville MarylandDave Flanagan Bowie MarylandBob Knox Ft. Lauderdale Florida%
All of the code that comprises the examples in this book may be found online and can be downloaded. In addition, each chapter has some exercises that support the topics in the chapters. These solutions can also be found online at http://www.phptr.com/saltzman/