The Norse God Odin had two ravens, Hugin and Munin (Thought and Memory). He would send them out each day to fly to the corners of the earth. At night, they would return and tell him all their secrets. Odin knew how to manage his resources. Thought and memory, cogitation and recall, processing and storage--as a programmer, these are the important resources you too must tame. This book aims to be your guide in this endeavor. By the time you finish this book, you should have the skills to manage your own Hugin and Munin. In other words, you will be able to write your own hugin Perl program to scour the web for interesting information, as well as a munin program to manage and query the database of information you collect.
There are a lot of books about Perl on the market today, and some of them I recommend highly. (See Appendix C, "Additional resources.") However, many authors of these other Perl books assume readers are already familiar with programming. Other authors take the side-effect approach, teaching readers the vocabulary and syntax of the language but offering few guidelines on how to use it effectively. I do not believe that the side-effect approach is an effective means of teaching programming.
This book instead presents the basic elements of programming using the context of the Perl language. I do not assume that you've programmed before, nor do I merely hammer you with syntax and function names. This book is designed to teach you both programming and Perl, from the basics to the more advanced skills you need to become an accomplished Perl programmer.
This book is intended for two types of readers: those approaching Perl as their first programming language and those who may have learned programming off the cuff but now want a more thorough grounding in programming in general, and Perl in particular.
More people than ever are learning Perl. Undoubtedly, Perl's widespread use for Common Gateway Interface (CGI) and web-client programming contributes to its popularity. Some people need Perl skills for their jobs, while others just think Perl programming is cool. Whatever your motivation, you need to understand up front that this book is not about using Perl for web-related programming, although an example or two illustrates that application of Perl. Instead, this book is about learning how to program using Perl. Once you have that knowledge under your belt, you can apply it to a multitude of problem domains.
This book does not assume that you know what variables, arrays, and loops are, or that you've programmed before. However, familiarity with basic mathematical concepts and logic will certainly be helpful. Readers with no prior programming experience should, of course, begin at the beginning and work their way through the first nine chapters in order. Chapters 10 through 15 are largely independent and can be read in any order. Chapters 16 through 19 introduce advanced Perl concepts. Each of these chapters lays a foundation for the following chapter, so read these four chapters in order.
If you are already familiar with elementary Perl programming, you may want to read chapters 2 and 3, and then pick and choose chapters that tackle areas in which you wish to improve. For example, chapters 6, 10, and 11 cover different aspects of regular expressions and matching operators. Chapter 8 covers references.
If you are a competent programmer in another language, this book may still be useful in demonstrating Perl's way of doing things. However, the discussions may not be as concise as you'd like, and the content is not organized as a reference book.
The book is organized in four main parts, starting with things to consider before you begin programming, followed by the essential aspects of programming with Perl. The third section explores a few of the more practical and Perl-specific areas. Finally, the later chapters introduce more advanced concepts, such as abstract data structures and object oriented-programming using Perl.
The three chapters in this section provide elementary information on programming and the Perl language. Chapters 2 and 3 also delve into the basics of program structure and design. In chapter 3, we work through two examples, providing a whirlwind tour of the Perl language in the process.
Chapters 4 through 9 cover the essential concepts and structures you need to learn to program effectively. Here you will find everything from variables to loop control constructs to file input and output to basic regular expressions to subroutines to references and nested data structures. When you finish this section of the book, you will have all the tools you need to build real-world applications.
Chapters 10 through 15 take you into areas more specific to the Perl language, exploiting some of Perl's unique and powerful strengths. Here we explore regular expressions in more detail, string and list processing, more input and output techniques, using modules, and the Perl debugger.
Chapters 16 through 19 provide an introduction to more advanced programming techniques, including building modules and abstract data structures. You are also introduced to object-oriented programming features in Perl. Chapter 20 mentions a few areas not covered in this book and suggests references for further study.
The four short appendices cover command-line switches, special Perl variables, additional resources for readers, and a brief explanation of binary, octal, and hexadecimal numeric representation. Following the appendices is a small glossary of technical terms used in this book.
Source code, solutions, and errata
The source code for many of the example programs and modules presented in this book may be obtained from Manning's website. Point your browser to http:// www.manning.com/Johnson for links to the online resources for the book, including source packages.
Many chapters have a small number of exercises at the end. There is no appendix of answers to these exercises. However, the web page mentioned above contains a link to a solutions page.
Finally, although we have strived to eliminate mistakes from the manuscript, some errors may have slipped through. The previously mentioned web page contains a link to an online errata listing corrections to errors discovered after the book was published. If you find any errors, please let us know so we can list them on the errata sheet and fix them in a later printing. The errata page lists an email address to which you can submit error reports.
In this book, "Perl" (uppercase P) refers to the Perl programming language, while "perl" (lowercase p) refers to the perl compiler/interpreter or the perl distribution.
Filenames and URLs appear in italics. Code, program names, and any commands you might issue at the command line prompt appear in a fixed-width font. Some blocks of code are written using a form of literate programming (LP) syntax to break the code into smaller chunks for presentation (explained in chapters 3 and 9). In these cases, the real Perl code, or the pseudo-code, is in a plain fixed-width font, while the lines representing literate programming syntax are in an italic fixed-width font.
Many of the technical terms introduced in this book are defined in the glossary. When the terms first appear in the text, they are italicized.
You will also see the words foo and bar throughout this book. These are generic terms commonly used in syntax examples to represent the name of a variable or the contents of a string. They are used when the point of the example is not directly related to whatever is being called "foo." You might encounter these terms--and many similar "dummy" words, such as foobar, baz, qux, and quux--in publicly available articles and examples on programming.