Cover image for Learning Perl, 3rd Edition

Book description

If you ask Perl programmers today what book they relied on most when they were learning Perl, you'll find that an overwhelming majority will name Learning Perl--also known affectionately as "the Llama." The first edition of Learning Perl appeared in 1993 and has been a bestseller ever since. Written by two of the most prominent and active members of the Perl community, this book is the quintessential tutorial for the Perl programming language. Perl began as a tool for Unix system administrators, used for countless small tasks throughout the workday. It has since blossomed into a full-featured programming language on practically every computing platform, and is used for web programming, database manipulation, XML processing, and (of course) system administration--all this while still remaining the perfect tool for the small daily tasks it was designed for. Perl is quick, fun, and eminently useful. Many people start using Perl because they need it, but they continue to use Perl because they love it. The third edition of Learning Perl has not only been updated for Perl 5.6, but has also been rewritten from the ground up to reflect the needs of programmers learning Perl today. Informed by their years of success at teaching Perl as consultants, the authors have re-engineered the book to better match the pace and scope appropriate for readers trying to get started with Perl, while retaining the detailed discussion, thorough examples, and eclectic wit for which the book is famous. This edition of the Llama includes an expanded and more gently-paced introduction to regular expressions, new exercises and solutions designed so readers can practice what they've learned while it's still fresh in their minds, and an overall reworking to bring Learning Perl into the new millennium. Perl is a language for getting your job done. Other books may teach you to program in Perl, but this book will turn you into a Perl programmer.

Table of Contents

  1. Learning Perl, 3rd Edition
  2. A Note Regarding Supplemental Files
  3. Preface
    1. Typographical Conventions
    2. How to Contact Us
    3. Code Examples
    4. History of This Book
    5. Acknowledgments
  4. 1. Introduction
    1. 1.1. Questions and Answers
      1. 1.1.1. Is This the Right Book for You?
      2. 1.1.2. Why Are There So Many Footnotes?
      3. 1.1.3. What About the Exercises and Their Answers?
      4. 1.1.4. What Do Those Numbers Mean at the Start of the Exercise?
      5. 1.1.5. What If I'm a Perl Course Instructor?
    2. 1.2. What Does "Perl" Stand For?
      1. 1.2.1. Why Didn't Larry Just Use Some Other Language?
      2. 1.2.2. Is Perl Easy or Hard?
      3. 1.2.3. How Did Perl Get to Be So Popular?
      4. 1.2.4. What's Happening with Perl Now?
      5. 1.2.5. What's Perl Really Good For?
      6. 1.2.6. What Is Perl Not Good For?
    3. 1.3. How Can I Get Perl?
      1. 1.3.1. What Is CPAN?
      2. 1.3.2. How Can I Get Support for Perl?
      3. 1.3.3. Are There Any Other Kinds of Support?
      4. 1.3.4. What If I Find a Bug in Perl?
    4. 1.4. How Do I Make a Perl Program?
      1. 1.4.1. A Simple Program
      2. 1.4.2. What's Inside That Program?
      3. 1.4.3. But How Do I Compile Perl?
    5. 1.5. A Whirlwind Tour of Perl
    6. 1.6. Exercises
  5. 2. Scalar Data
    1. 2.1. What Is Scalar Data?
    2. 2.2. Numbers
      1. 2.2.1. All Numbers Are the Same Format Internally
      2. 2.2.2. Floating-Point Literals
      3. 2.2.3. Integer Literals
      4. 2.2.4. Nondecimal Integer Literals
      5. 2.2.5. Numeric Operators
    3. 2.3. Strings
      1. 2.3.1. Single-Quoted String Literals
      2. 2.3.2. Double-Quoted String Literals
      3. 2.3.3. String Operators
      4. 2.3.4. Automatic Conversion Between Numbers and Strings
    4. 2.4. Perl's Built-in Warnings
    5. 2.5. Scalar Variables
      1. 2.5.1. Choosing Good Variable Names
      2. 2.5.2. Scalar Assignment
      3. 2.5.3. Binary Assignment Operators
    6. 2.6. Output with print
      1. 2.6.1. Interpolation of Scalar Variables into Strings
      2. 2.6.2. Operator Precedence and Associativity
      3. 2.6.3. Comparison Operators
    7. 2.7. The if Control Structure
      1. 2.7.1. Boolean Values
    8. 2.8. Getting User Input
    9. 2.9. The chomp Operator
    10. 2.10. The while Control Structure
    11. 2.11. The undef Value
    12. 2.12. The defined Function
    13. 2.13. Exercises
  6. 3. Lists and Arrays
    1. 3.1. Accessing Elements of an Array
    2. 3.2. Special Array Indices
    3. 3.3. List Literals
      1. 3.3.1. The qw Shortcut
    4. 3.4. List Assignment
      1. 3.4.1. The pop and push Operators
      2. 3.4.2. The shift and unshift Operators
    5. 3.5. Interpolating Arrays into Strings
    6. 3.6. The foreach Control Structure
    7. 3.7. Perl's Favorite Default: $_
      1. 3.7.1. The reverse Operator
      2. 3.7.2. The sort Operator
    8. 3.8. Scalar and List Context
      1. 3.8.1. Using List-Producing Expressions in Scalar Context
      2. 3.8.2. Using Scalar-Producing Expressions in List Context
      3. 3.8.3. Forcing Scalar Context
    9. 3.9. <STDIN> in List Context
    10. 3.10. Exercises
  7. 4. Subroutines
    1. 4.1. System and User Functions
    2. 4.2. Defining a Subroutine
    3. 4.3. Invoking a Subroutine
    4. 4.4. Return Values
    5. 4.5. Arguments
    6. 4.6. Private Variables in Subroutines
    7. 4.7. The local Operator
      1. 4.7.1. The Difference Between local and my
    8. 4.8. Variable-length Parameter Lists
      1. 4.8.1. A Better &max Routine
      2. 4.8.2. Empty Parameter Lists
    9. 4.9. Notes on Lexical (my) Variables
    10. 4.10. The use strict Pragma
    11. 4.11. The return Operator
      1. 4.11.1. Omitting the Ampersand
    12. 4.12. Exercises
  8. 5. Hashes
    1. 5.1. What Is a Hash?
      1. 5.1.1. Why Use a Hash?
    2. 5.2. Hash Element Access
      1. 5.2.1. The Hash as a Whole
      2. 5.2.2. Hash Assignment
      3. 5.2.3. The Big Arrow
    3. 5.3. Hash Functions
      1. 5.3.1. The keys and values Functions
      2. 5.3.2. The each Function
    4. 5.4. Typical Use of a Hash
      1. 5.4.1. The exists Function
      2. 5.4.2. The delete Function
      3. 5.4.3. Hash Element Interpolation
    5. 5.5. Exercises
  9. 6. I/O Basics
    1. 6.1. Input from Standard Input
    2. 6.2. Input from the Diamond Operator
    3. 6.3. The Invocation Arguments
    4. 6.4. Output to Standard Output
    5. 6.5. Formatted Output with printf
      1. 6.5.1. Arrays and printf
    6. 6.6. Exercises
  10. 7. Concepts of Regular Expressions
    1. 7.1. What Are Regular Expressions?
    2. 7.2. Using Simple Patterns
      1. 7.2.1. About Metacharacters
      2. 7.2.2. Simple Quantifiers
      3. 7.2.3. Grouping in Patterns
      4. 7.2.4. Alternatives
    3. 7.3. A Pattern Test Program
    4. 7.4. Exercises
  11. 8. More About Regular Expressions
    1. 8.1. Character Classes
      1. 8.1.1. Character Class Shortcuts
      2. 8.1.2. Negating the Shortcuts
    2. 8.2. General Quantifiers
    3. 8.3. Anchors
      1. 8.3.1. Word Anchors
    4. 8.4. Memory Parentheses
      1. 8.4.1. Backreferences
      2. 8.4.2. Memory Variables
    5. 8.5. Precedence
      1. 8.5.1. Examples of Precedence
      2. 8.5.2. And There's More
    6. 8.6. Exercises
  12. 9. Using Regular Expressions
    1. 9.1. Matches with m//
    2. 9.2. Option Modifiers
      1. 9.2.1. Case-insensitive Matching with /i
      2. 9.2.2. Matching Any Character with /s
      3. 9.2.3. Combining Option Modifiers
      4. 9.2.4. Other Options
    3. 9.3. The Binding Operator, =~
    4. 9.4. Interpolating into Patterns
    5. 9.5. The Match Variables
      1. 9.5.1. The Persistence of Memory
      2. 9.5.2. The Automatic Match Variables
    6. 9.6. Substitutions with s///
      1. 9.6.1. Global Replacements with /g
      2. 9.6.2. Different Delimiters
      3. 9.6.3. Option Modifiers
      4. 9.6.4. The Binding Operator
      5. 9.6.5. Case Shifting
    7. 9.7. The split Operator
    8. 9.8. The join Function
    9. 9.9. Exercises
  13. 10. More Control Structures
    1. 10.1. The unless Control Structure
      1. 10.1.1. The else Clause with unless
    2. 10.2. The until Control Structure
    3. 10.3. Expression Modifiers
    4. 10.4. The Naked Block Control Structure
    5. 10.5. The elsif Clause
    6. 10.6. Autoincrement and Autodecrement
      1. 10.6.1. The Value of Autoincrement
    7. 10.7. The for Control Structure
      1. 10.7.1. The Secret Connection Between foreach and for
    8. 10.8. Loop Controls
      1. 10.8.1. The last Operator
      2. 10.8.2. The next Operator
      3. 10.8.3. The redo Operator
      4. 10.8.4. Labeled Blocks
    9. 10.9. Logical Operators
      1. 10.9.1. The Value of a Short-Circuit Operator
      2. 10.9.2. The Ternary Operator, ?:
      3. 10.9.3. Control Structures Using Partial-Evaluation Operators
    10. 10.10. Exercise
  14. 11. Filehandles and File Tests
    1. 11.1. What Is a Filehandle?
    2. 11.2. Opening a Filehandle
      1. 11.2.1. Closing a Filehandle
      2. 11.2.2. Bad Filehandles
    3. 11.3. Fatal Errors with die
      1. 11.3.1. Warning Messages with warn
    4. 11.4. Using Filehandles
      1. 11.4.1. Changing the Default Output Filehandle
    5. 11.5. Reopening a Standard Filehandle
    6. 11.6. File Tests
      1. 11.6.1. The stat and lstat Functions
      2. 11.6.2. The localtime Function
      3. 11.6.3. Bitwise Operators
      4. 11.6.4. Using Bitstrings
      5. 11.6.5. Using the Special Underscore Filehandle
    7. 11.7. Exercises
  15. 12. Directory Operations
    1. 12.1. Moving Around the Directory Tree
    2. 12.2. Globbing
    3. 12.3. An Alternate Syntax for Globbing
    4. 12.4. Directory Handles
    5. 12.5. Recursive Directory Listing
    6. 12.6. Exercises
  16. 13. Manipulating Files and Directories
    1. 13.1. Removing Files
    2. 13.2. Renaming Files
    3. 13.3. Links and Files
    4. 13.4. Making and Removing Directories
    5. 13.5. Modifying Permissions
    6. 13.6. Changing Ownership
    7. 13.7. Changing Timestamps
    8. 13.8. Using Simple Modules
      1. 13.8.1. The File::Basename Module
      2. 13.8.2. Using Only Some Functions from a Module
      3. 13.8.3. The File::Spec Module
    9. 13.9. Exercises
  17. 14. Process Management
    1. 14.1. The system Function
      1. 14.1.1. Avoiding the Shell
    2. 14.2. The exec Function
    3. 14.3. The Environment Variables
    4. 14.4. Using Backquotes to Capture Output
      1. 14.4.1. Using Backquotes in a List Context
    5. 14.5. Processes as Filehandles
    6. 14.6. Getting Down and Dirty with Fork
    7. 14.7. Sending and Receiving Signals
    8. 14.8. Exercises
  18. 15. Strings and Sorting
    1. 15.1. Finding a Substring with index
    2. 15.2. Manipulating a Substring with substr
    3. 15.3. Formatting Data with sprintf
      1. 15.3.1. Using sprintf with "Money Numbers"
    4. 15.4. Advanced Sorting
      1. 15.4.1. Sorting a Hash by Value
      2. 15.4.2. Sorting by Multiple Keys
    5. 15.5. Exercises
  19. 16. Simple Databases
    1. 16.1. DBM Files and DBM Hashes
      1. 16.1.1. Opening and Closing DBM Hashes
      2. 16.1.2. Using a DBM Hash
    2. 16.2. Manipulating Data with pack and unpack
    3. 16.3. Fixed-length Random-access Databases
    4. 16.4. Variable-length (Text) Databases
      1. 16.4.1. In-place Editing from the Command Line
    5. 16.5. Exercises
  20. 17. Some Advanced Perl Techniques
    1. 17.1. Trapping Errors with eval
    2. 17.2. Picking Items from a List with grep
    3. 17.3. Transforming Items from a List with map
    4. 17.4. Unquoted Hash Keys
    5. 17.5. More Powerful Regular Expressions
      1. 17.5.1. Non-greedy Quantifiers
      2. 17.5.2. Matching Multiple-line Text
    6. 17.6. Slices
      1. 17.6.1. Array Slice
      2. 17.6.2. Hash Slice
    7. 17.7. Exercise
  21. A. Exercise Answers
    1. A.1. Answers to Chapter 2 Exercises
    2. A.2. Answers to Chapter 3 Exercises
    3. A.3. Answers to Chapter 4 Exercises
    4. A.4. Answers to Chapter 5 Exercises
    5. A.5. Answers to Chapter 6 Exercises
    6. A.6. Answers to Chapter 7 Exercises
    7. A.7. Answers to Chapter 8 Exercises
    8. A.8. Answers to Chapter 9 Exercises
    9. A.9. Answer to Chapter 10 Exercise
    10. A.10. Answers to Chapter 11 Exercises
    11. A.11. Answers to Chapter 12 Exercises
    12. A.12. Answers to Chapter 13 Exercises
    13. A.13. Answers to Chapter 14 Exercises
    14. A.14. Answers to Chapter 15 Exercises
    15. A.15. Answers to Chapter 16 Exercises
    16. A.16. Answer to Chapter 17 Exercises
  22. B. Beyond the Llama
    1. B.1. Further Documentation
    2. B.2. Regular expressions
    3. B.3. Packages
    4. B.4. Extending Perl's Functionality
      1. B.4.1. Libraries
      2. B.4.2. Modules
      3. B.4.3. Finding and Installing Modules
      4. B.4.4. Writing Your Own Modules
    5. B.5. Some Important Modules
      1. B.5.1. The CGI and CGI_Lite Modules
      2. B.5.2. The Cwd Module
      3. B.5.3. The Fatal Module
      4. B.5.4. The File::Basename Module
      5. B.5.5. The File::Copy Module
      6. B.5.6. The File::Spec Module
      7. B.5.7. The Image::Size Module
      8. B.5.8. The Net::SMTP Module
      9. B.5.9. The POSIX Module
      10. B.5.10. The Sys::Hostname Module
      11. B.5.11. The Text::Wrap Module
      12. B.5.12. The Time::Local Module
    6. B.6. Pragmas
      1. B.6.1. The constant Pragma
      2. B.6.2. The diagnostics Pragma
      3. B.6.3. The lib Pragma
      4. B.6.4. The strict Pragma
      5. B.6.5. The vars Pragma
      6. B.6.6. The warnings Pragma
    7. B.7. Databases
      1. B.7.1. Direct System Database Access
      2. B.7.2. Flat-file Database Access
      3. B.7.3. Relational Database Access
    8. B.8. Other Operators and Functions
      1. B.8.1. Transliteration with tr///
      2. B.8.2. Here documents
    9. B.9. Mathematics
      1. B.9.1. Advanced Math Functions
      2. B.9.2. Imaginary and Complex Numbers
      3. B.9.3. Large and High-Precision Numbers
    10. B.10. Lists and Arrays
      1. B.10.1. map and grep
      2. B.10.2. The splice Operator
    11. B.11. Bits and Pieces
    12. B.12. Formats
    13. B.13. Networking and IPC
      1. B.13.1. System V IPC
      2. B.13.2. Sockets
    14. B.14. Security
    15. B.15. Debugging
    16. B.16. The Common Gateway Interface (CGI)
    17. B.17. Command-Line Options
    18. B.18. Built in Variables
    19. B.19. Syntax Extensions
    20. B.20. References
      1. B.20.1. Complex Data Structures
      2. B.20.2. Object-Oriented Programming
      3. B.20.3. Anonymous Subroutines and Closures
    21. B.21. Tied Variables
    22. B.22. Operator Overloading
    23. B.23. Dynamic Loading
    24. B.24. Embedding
    25. B.25. Converting Other Languages to Perl
    26. B.26. Converting find Command Lines to Perl
    27. B.27. Command-line Options in Your Programs
    28. B.28. Embedded Documentation
    29. B.29. More Ways to Open Filehandles
    30. B.30. Locales and Unicode
    31. B.31. Threads and Forking
    32. B.32. Graphical User Interfaces (GUIs)
    33. B.33. And More...
  23. Index
  24. About the Authors
  25. Colophon
  26. Copyright