Cover image for Learning Perl, 6th Edition

Book description

Popularly known as "the Llama," Learning Perl is the book most programmers rely on to get started with this versatile language. The bestselling Perl tutorial since it was first published in 1993, this sixth edition includes recent changes to the language up to Perl 5.14 as well as a major overhaul for Unicode. Reflecting years of classroom testing and experience, this edition is packed with exercises that let you practice the concepts while you follow the text.

Table of Contents

  1. Learning Perl
  2. Preface
    1. Typographical Conventions
    2. Code Examples
    3. Safari® Books Online
    4. How to Contact Us
    5. History of This Book
    6. Changes from the Previous Edition
    7. Acknowledgments
      1. From Randal
      2. From Tom
      3. From brian
      4. From All of Us
  3. 1. Introduction
    1. Questions and Answers
      1. Is This the Right Book for You?
      2. Why Are There So Many Footnotes?
      3. What About the Exercises and Their Answers?
      4. What Do Those Numbers Mean at the Start of the Exercise?
      5. What If I’m a Perl Course Instructor?
    2. What Does “Perl” Stand For?
      1. Why Did Larry Create Perl?
      2. Why Didn’t Larry Just Use Some Other Language?
      3. Is Perl Easy or Hard?
      4. How Did Perl Get to Be So Popular?
      5. What’s Happening with Perl Now?
      6. What’s Perl Really Good For?
      7. What Is Perl Not Good For?
    3. How Can I Get Perl?
      1. What Is CPAN?
      2. How Can I Get Support for Perl?
      3. Are There Any Other Kinds of Support?
      4. What If I Find a Bug in Perl?
    4. How Do I Make a Perl Program?
      1. A Simple Program
      2. What’s Inside That Program?
      3. How Do I Compile My Perl Program?
    5. A Whirlwind Tour of Perl
    6. Exercises
  4. 2. Scalar Data
    1. Numbers
      1. All Numbers Have the Same Format Internally
      2. Floating-Point Literals
      3. Integer Literals
      4. Nondecimal Integer Literals
      5. Numeric Operators
    2. Strings
      1. Single-Quoted String Literals
      2. Double-Quoted String Literals
      3. String Operators
      4. Automatic Conversion Between Numbers and Strings
    3. Perl’s Built-in Warnings
    4. Scalar Variables
      1. Choosing Good Variable Names
      2. Scalar Assignment
      3. Binary Assignment Operators
    5. Output with print
      1. Interpolation of Scalar Variables into Strings
      2. Creating Characters by Code Point
      3. Operator Precedence and Associativity
      4. Comparison Operators
    6. The if Control Structure
      1. Boolean Values
    7. Getting User Input
    8. The chomp Operator
    9. The while Control Structure
    10. The undef Value
    11. The defined Function
    12. Exercises
  5. 3. Lists and Arrays
    1. Accessing Elements of an Array
    2. Special Array Indices
    3. List Literals
      1. The qw Shortcut
    4. List Assignment
      1. The pop and push Operators
      2. The shift and unshift Operators
      3. The splice Operator
    5. Interpolating Arrays into Strings
    6. The foreach Control Structure
      1. Perl’s Favorite Default: $_
      2. The reverse Operator
      3. The sort Operator
      4. The each Operator
    7. Scalar and List Context
      1. Using List-Producing Expressions in Scalar Context
      2. Using Scalar-Producing Expressions in List Context
      3. Forcing Scalar Context
    8. <STDIN> in List Context
    9. Exercises
  6. 4. Subroutines
    1. Defining a Subroutine
    2. Invoking a Subroutine
    3. Return Values
    4. Arguments
    5. Private Variables in Subroutines
    6. Variable-Length Parameter Lists
      1. A Better &max Routine
      2. Empty Parameter Lists
    7. Notes on Lexical (my) Variables
    8. The use strict Pragma
    9. The return Operator
      1. Omitting the Ampersand
    10. Non-Scalar Return Values
    11. Persistent, Private Variables
    12. Exercises
  7. 5. Input and Output
    1. Input from Standard Input
    2. Input from the Diamond Operator
    3. The Invocation Arguments
    4. Output to Standard Output
    5. Formatted Output with printf
      1. Arrays and printf
    6. Filehandles
    7. Opening a Filehandle
      1. Binmoding Filehandles
      2. Bad Filehandles
      3. Closing a Filehandle
    8. Fatal Errors with die
      1. Warning Messages with warn
      2. Automatically die-ing
    9. Using Filehandles
      1. Changing the Default Output Filehandle
    10. Reopening a Standard Filehandle
    11. Output with say
    12. Filehandles in a Scalar
    13. Exercises
  8. 6. Hashes
    1. What Is a Hash?
      1. Why Use a Hash?
    2. Hash Element Access
      1. The Hash As a Whole
      2. Hash Assignment
      3. The Big Arrow
    3. Hash Functions
      1. The keys and values Functions
      2. The each Function
    4. Typical Use of a Hash
      1. The exists Function
      2. The delete Function
      3. Hash Element Interpolation
    5. The %ENV hash
    6. Exercises
  9. 7. In the World of Regular Expressions
    1. What Are Regular Expressions?
    2. Using Simple Patterns
      1. Unicode Properties
      2. About Metacharacters
      3. Simple Quantifiers
      4. Grouping in Patterns
      5. Alternatives
    3. Character Classes
      1. Character Class Shortcuts
      2. Negating the Shortcuts
    4. Exercises
  10. 8. Matching with Regular Expressions
    1. Matches with m//
    2. Match Modifiers
      1. Case-Insensitive Matching with /i
      2. Matching Any Character with /s
      3. Adding Whitespace with /x
      4. Combining Option Modifiers
      5. Choosing a Character Interpretation
      6. Other Options
    3. Anchors
      1. Word Anchors
    4. The Binding Operator =~
    5. Interpolating into Patterns
    6. The Match Variables
      1. The Persistence of Captures
      2. Noncapturing Parentheses
      3. Named Captures
      4. The Automatic Match Variables
    7. General Quantifiers
    8. Precedence
      1. Examples of Precedence
      2. And There’s More
    9. A Pattern Test Program
    10. Exercises
  11. 9. Processing Text with Regular Expressions
    1. Substitutions with s///
      1. Global Replacements with /g
      2. Different Delimiters
      3. Substitution Modifiers
      4. The Binding Operator
      5. Nondestructive Substitutions
      6. Case Shifting
    2. The split Operator
    3. The join Function
    4. m// in List Context
    5. More Powerful Regular Expressions
      1. Nongreedy Quantifiers
      2. Matching Multiple-Line Text
      3. Updating Many Files
      4. In-Place Editing from the Command Line
    6. Exercises
  12. 10. More Control Structures
    1. The unless Control Structure
      1. The else Clause with unless
    2. The until Control Structure
    3. Expression Modifiers
    4. The Naked Block Control Structure
    5. The elsif Clause
    6. Autoincrement and Autodecrement
      1. The Value of Autoincrement
    7. The for Control Structure
      1. The Secret Connection Between foreach and for
    8. Loop Controls
      1. The last Operator
      2. The next Operator
      3. The redo Operator
      4. Labeled Blocks
    9. The Conditional Operator ?:
    10. Logical Operators
      1. The Value of a Short Circuit Operator
      2. The defined-or Operator
      3. Control Structures Using Partial-Evaluation Operators
    11. Exercises
  13. 11. Perl Modules
    1. Finding Modules
    2. Installing Modules
      1. Using Your Own Directories
    3. Using Simple Modules
      1. The File::Basename Module
      2. Using Only Some Functions from a Module
      3. The File::Spec Module
      4. Path::Class
      5. CGI.pm
      6. Databases and DBI
      7. Dates and Times
    4. Exercises
  14. 12. File Tests
    1. File Test Operators
      1. Testing Several Attributes of the Same File
      2. Stacked File Test Operators
    2. The stat and lstat Functions
    3. The localtime Function
    4. Bitwise Operators
      1. Using Bitstrings
    5. Exercises
  15. 13. Directory Operations
    1. Moving Around the Directory Tree
    2. Globbing
    3. An Alternate Syntax for Globbing
    4. Directory Handles
    5. Recursive Directory Listing
    6. Manipulating Files and Directories
    7. Removing Files
    8. Renaming Files
    9. Links and Files
    10. Making and Removing Directories
    11. Modifying Permissions
    12. Changing Ownership
    13. Changing Timestamps
    14. Exercises
  16. 14. Strings and Sorting
    1. Finding a Substring with index
    2. Manipulating a Substring with substr
    3. Formatting Data with sprintf
      1. Using sprintf with “Money Numbers”
      2. Interpreting Non-Decimal Numerals
    4. Advanced Sorting
      1. Sorting a Hash by Value
      2. Sorting by Multiple Keys
    5. Exercises
  17. 15. Smart Matching and given-when
    1. The Smart Match Operator
    2. Smart Match Precedence
    3. The given Statement
      1. Dumb Matching
    4. Using when with Many Items
    5. Exercises
  18. 16. Process Management
    1. The system Function
      1. Avoiding the Shell
    2. The Environment Variables
    3. The exec Function
    4. Using Backquotes to Capture Output
      1. Using Backquotes in a List Context
    5. External Processes with IPC::System::Simple
    6. Processes as Filehandles
    7. Getting Down and Dirty with Fork
    8. Sending and Receiving Signals
    9. Exercises
  19. 17. Some Advanced Perl Techniques
    1. Slices
      1. Array Slice
      2. Hash Slice
    2. Trapping Errors
      1. Using eval
      2. More Advanced Error Handling
      3. autodie
    3. Picking Items from a List with grep
    4. Transforming Items from a List with map
    5. Fancier List Utilities
    6. Exercises
  20. A. Exercise Answers
    1. Answers to Chapter 1 Exercises
    2. Answers to Chapter 2 Exercises
    3. Answers to Chapter 3 Exercises
    4. Answers to Chapter 4 Exercises
    5. Answers to Chapter 5 Exercises
    6. Answers to Chapter 6 Exercises
    7. Answers to Chapter 7 Exercises
    8. Answers to Chapter 8 Exercises
    9. Answers to Chapter 9 Exercises
    10. Answers to Chapter 10 Exercises
    11. Answer to Chapter 11 Exercises
    12. Answers to Chapter 12 Exercises
    13. Answers to Chapter 13 Exercises
    14. Answers to Chapter 14 Exercises
    15. Answers to Chapter 15 Exercises
    16. Answers to Chapter 16 Exercises
    17. Answer to Chapter 17 Exercises
  21. B. Beyond the Llama
    1. Further Documentation
    2. Regular Expressions
    3. Packages
    4. Extending Perl’s Functionality
      1. Libraries
      2. Writing Your Own Modules
    5. Databases
      1. Direct System Database Access
      2. Flat-File Database Access
    6. Other Operators and Functions
      1. Transliteration with tr///
      2. Here Documents
    7. Mathematics
      1. Advanced Math Functions
      2. Imaginary and Complex Numbers
      3. Large and High-Precision Numbers
    8. Lists and Arrays
      1. map and grep
    9. Bits and Pieces
    10. Formats
    11. Networking and IPC
      1. System V IPC
      2. Sockets
    12. Security
    13. Debugging
    14. Command-Line Options
    15. Built-in Variables
    16. Syntax Extensions
    17. References
      1. Complex Data Structures
      2. Object-Oriented Programming
      3. Anonymous Subroutines and Closures
    18. Tied Variables
    19. Operator Overloading
    20. Dynamic Loading
    21. Embedding
    22. Converting Other Languages to Perl
    23. Converting find Command Lines to Perl
    24. Command-Line Options in Your Programs
    25. Embedded Documentation
    26. More Ways to Open Filehandles
    27. Threads and Forking
    28. Graphical User Interfaces (GUIs)
    29. And More…
  22. C. A Unicode Primer
    1. Unicode
    2. UTF-8 and Friends
    3. Getting Everyone to Agree
    4. Fancy Characters
      1. Fancier Characters
    5. Dealing with Unicode in Perl
      1. Using Unicode in Your Source
      2. Fancier Characters by Name
      3. Reading from STDIN or Writing to STDOUT or STDERR
      4. Reading from and Writing to Files
      5. Dealing with Command-Line Arguments
      6. Dealing with Databases
    6. Further Reading
  23. Index
  24. About the Authors
  25. Colophon
  26. Copyright