The serious developer's guide to Perl!
This start-to-finish tutorial and reference gives experienced developers the techniques and real-world code they need to master Perlfast! Begin by leveraging your current skills to quickly master the fundamentals of Perl developmentfrom basic syntax to objects, data structures to text processing. Next, learn how to use Perl's powerful facilities for working with files, networks, and relational databases. Finally, Core Perl shows you how to make the most of Perl in any Web application-from simple CGI to full-fledged site development and delivery. Coverage includes all this and more:
For years, developers worldwide have relied on Perl for fast, flexible solutions to an extraordinary range of problems, from text manipulation and system administration to database access and Web integration. Core Perl gives developers the skills and insight needed to build robust, maintainable, high-performance codewhatever your application.
(NOTE: Each chapter concludes with a Conclusion.)
1. What is Perl?
What is Perl? When not to use Perl. The Perl license. Perl versions and standardization. Perl support. Retrieving and installing Perl. Retrieving modules from CPAN.
Basic programs. Compiler or interpreter? Statements and blocks. Variables. Scalars. Lists and arrays. Hashes. References. Lexicals and globals.
Basic output. Time. Basic input. Conditional execution. Comparison operators. Logical operators. Order of operations. Loops. Sorting. Files. Built-in variables. Data manipulation functions. Running external programs. fork. eval.
Subroutine basics. Return values. Variables within subroutines. Subroutine arguments. Subroutine references. BEGI and END. Signals.
What are text patterns? Metacharacters. Anchoring searches. Character classes. Predefined character classes. Parentheses. Grabbing parts of a string. Substitution. Greediness. Options for matching and replacing. study. Regexp objects. tr///. Substituting across multiple files. grep.
Packages. Modules. Exporting symbols. Some example modules. Documenting modules with POD.
Objects. Methods. Inheritance. Creating and working with objects.
Tying. Tying a scalar. Tying a hash. Tying an array.
Basic file access. Random file access. Filehandles. Globbing. Real and effective IDs. File permissions. suid and sgid. Unary file tests. stat. Locking. Directories. Renaming and deleting. Links.
Pipes. Network communication. Working with Internet protocols.
What is a relational database? Introducing SQL. SELECT. Joins. Updating and deleting. Indexes. Perl and databases. Simple DBI programs.
Database design. Writing the applications. Debugging DBI.
Identifying problems. When things go wrong. Tainting. Debugging. Benchmarking.
Dynamic Web content. CGI. Error messages and debugging. Redirection. More CGI methods.
User registration. Cookies. Graphic output. Templates.
Stock updates. Postcards. Personalization with databases.
Installing and configuring mod perl. Configuration directives. Three simple handlers. Apache: Registry. Useful modules.
Mason. autohandler and dhandler. autohandler. Session management. Conclusion.
Perl books. Periodicals. World Wide Web. Databases. Useful Web sites.
Perl has been my programming language of choice since early 1992,when I began work on my undergraduate thesis. Part of the workrequired that I put 10 years of campus newspaper archives on theInternet using a system known as WAIS (Wide Area Information System).
For this project, I bought two books: One described sed and awk, the traditional Unix text-processing languages. The second described Perl, whichwas then relatively new. It was obvious that Perl was more advanced thanthe other two options, and I began to write small-and medium-sized Perlprograms to make the newspaper available online.
My affinity for Perl has increased significantly since my graduation fromcollege. I depend on it for much of my consulting work, as well as manypersonal projects. The speed with which I can produce a working program,and the wealth of modules available on the Internet, makes Perl a greatchoice.
Perl has matured greatly since I first read about it. Modern versions support object-oriented programming, complex data structures, modular code,and excellent diagnostic error messages. Perl now runs on nearly every operating system and comes standard with many. Perl is particularly popularamong Web site developers, but it can also be used to construct graphical user interfaces, database client programs, and network applications. ThePerl community has grown along with the language, spawning scores of newsgroups, mailing lists, books, magazine articles, and Web sites.If you are new to programming
Like other books in the Core series, Core Perl does not teach you how toprogram. It is meant to teach you enough about Perl syntax, functions, andidioms to begin writing intermediate and advanced programs. The numerousexamples are often described in great detail so that you can incorporate theminto your own programs. I have used drafts of this book in training my ownemployees, and have incorporated their feedback into the final product.Structure of the book
This book is divided into three parts:
Perl basics. We examine Perl syntax, beginning with the different majordata types (scalars, arrays, and hashes) and quickly moving onto ref-erences and complex data structures. We then discuss a number ofbuilt-in functions and variables, and how to use regular expressions toprocess text. This part of the book ends with chapters on subroutines,modules, and object-oriented syntax.
Using Perl. Perl has excellent facilities for working with files, networks,and relational databases. In this part of the book, we create programsthat exercise all of these features, creating simple network clients andservers, parsing text, and using Perl 's database interface (DBI). We alsodiscuss ways to improve and fix our programs, including the "tainting "mechanism, various command-line flags and the built-in Perl debugger.
The World Wide Web. Perl is an excellent and popular choice for server-side Web programs. We begin by looking at CGI, an old but popu-lar method for generating dynamic content. We also discuss HTTPcookies, HTML/Perl templates, and how to incorporate a relationaldatabase within a site. The final chapter describes the
mod perl modulefor Apache, ending with the Mason component-based Web publishingsystem.
Perl is a rich language, and it continues to grow daily. Consequently, no onebook can hope to cover everything. Here are some topics that Core Perldoes not discuss:
Tk and graphical user interfaces (GUIs). You can create GUIs withPerl using the cross-platform Tk toolkit. The programs in Core Perluse a command-line (text) interface, or are meant to be run as a server-side Web program.
Embedding Perl. It is possible to put Perl inside of a C or C++ program.This functionality is demonstrated best by
mod perl (see Chapter 17),but is also used by Oracle and other products. Core Perl does not discuss this subject, about which you can read in the
External linking with XS. XS allows Perl to use C-language librariesand forms the core of a number of important Perl modules, includ-ing DBI. We do not discuss XS in this book; see the standard
perlxstut manual pages if this subject interests you.
Compilation. Perl programs are traditionally compiled when they are invoked. The compiler turns a program into "opcodes," which are theninterpreted and executed by Perl. Many programmers would like atrue Perl compiler, which would produce standalone, precompiled programs. This goal is still somewhat far off, but the Perl compiler projectcontinues at a steady pace. Core Perl does not discuss compilation,but you can read more on this subject in the documentation for the
Threads. Perl grew up on Unix, which traditionally uses many processes.Modern operating systems allow for multiple threads of execution withina process, which is more efficient. Threading is still an experimentalfeature in Perl, and is not discussed in Core Perl. However, a tutorialon Perl threading is available in the online
Now that you know a bit about the book, let me tell you something aboutmyself: I have been working as a networking and Internet consultant since1995, when I moved from the United States to Israel. Since then, I haveworked on a variety of projects, most of which involve server-side Web applications using a relational database. Since January 1996,I have writtenthe monthly "At the Forge "column in Linux Journal, in which I exploreWeb-related technologies and programming techniques.
Before moving to Israel, I worked for Hewlett-Packard 's Medical Prod-ucts Group, connecting hospital devices to databases. I also worked forTime Inc.'s ill-fated Pathfinder Web site, helping such magazines as Fortune, Money, and Sports Illustrated for Kids produce their initial Web sites. At both jobs, I used Perl to solve a wide variety of problems, from analyzingerror logs to storing personalization information in a database.
I also rely on Perl for personal projects. The GNU Emacs FAQ, whichI edit, is maintained by a small set of Perl programs. The maintenancework that I do on a Web site and mailing list for residents of the Israelicity of Modi 'in is largely done in Perl. And, of course, Perl played a majorrole in the production of this book, uploading new drafts to my editors andextracting program fragments from the manuscript.Corrections and updates
Despite my best efforts, there are undoubtedly still some errors in this book.Please send corrections to
firstname.lastname@example.org; I will post updates on the CorePerl Web site, at
One of the mantras of the Perl community is that programming shouldbe fun. If this book makes it possible for you to work more efficiently andenjoyably, my mission will have been accomplished.