You are previewing Learning Perl, 7th Edition.
O'Reilly logo
Learning Perl, 7th 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.24. 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. 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 brian
      3. From Tom
      4. From All of Us
  2. 1. Introduction
    1. Questions and Answers
      1. Is This the Right Book for You?
      2. What About the Exercises and Their Answers?
      3. 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. Is There Any Kind of Support?
      3. 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
  3. 2. Scalar Data
    1. Numbers
      1. All Numbers Have the Same Format Internally
      2. Integer Literals
      3. Nondecimal Integer Literals
      4. Floating-Point 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
      1. Interpreting Nondecimal Numerals
    4. Scalar Variables
      1. Choosing Good Variable Names
      2. Scalar Assignment
      3. Compound 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
  4. 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
  5. 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. Nonscalar Return Values
    11. Persistent, Private Variables
    12. Subroutine Signatures
    13. Exercises
  6. 5. Input and Output
    1. Input from Standard Input
    2. Input from the Diamond Operator
      1. The Double Diamond
    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
  7. 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
  8. 7. Regular Expressions
    1. Sequences
    2. Practice Some Patterns
    3. The Wildcard
    4. Quantifiers
    5. Grouping in Patterns
    6. Alternatives
    7. Character Classes
      1. Character Class Shortcuts
      2. Negating the Shortcuts
    8. Unicode Properties
    9. Anchors
      1. Word Anchors
    10. Exercises
  9. 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. Beginning and End-of-Line Anchors
      7. Other Options
    3. The Binding Operator =~
    4. The Match Variables
      1. The Persistence of Captures
      2. Noncapturing Parentheses
      3. Named Captures
      4. The Automatic Match Variables
    5. Precedence
      1. Examples of Precedence
      2. And There’s More
    6. A Pattern Test Program
    7. Exercises
  10. 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
      7. Metaquoting
    2. The split Operator
    3. The join Function
    4. m// in List Context
    5. More Powerful Regular Expressions
      1. Nongreedy Quantifiers
      2. Fancier Word Boundaries
      3. Matching Multiple-Line Text
      4. Updating Many Files
      5. In-Place Editing from the Command Line
    6. Exercises
  11. 10. More Control Structures
    1. The unless Control Structure
      1. The else Clause with unless
    2. The until Control Structure
    3. Statement 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
  12. 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. Databases and DBI
      6. Dates and Times
    4. Exercises
  13. 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
  14. 13. Directory Operations
    1. The Current Working Directory
    2. Changing the Directory
    3. Globbing
    4. An Alternate Syntax for Globbing
    5. Directory Handles
    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
  15. 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”
    4. Advanced Sorting
      1. Sorting a Hash by Value
      2. Sorting by Multiple Keys
    5. Exercises
  16. 15. 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
  17. 16. Some Advanced Perl Techniques
    1. Slices
      1. Array Slice
      2. Hash Slice
      3. Key-Value Slices
    2. Trapping Errors
      1. Using eval
      2. More Advanced Error Handling
    3. Picking Items from a List with grep
    4. Transforming Items from a List with map
    5. Fancier List Utilities
    6. Exercises
  18. 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. Answers 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
  19. B. Beyond the Llama
    1. Further Documentation
    2. Regular Expressions
    3. Packages
    4. Extending Perl’s Functionality
      1. Writing Your Own Modules
    5. Databases
    6. Mathematics
    7. Lists and Arrays
    8. Bits and Pieces
    9. Formats
    10. Networking and IPC
      1. System V IPC
      2. Sockets
    11. Security
    12. Debugging
    13. Command-Line Options
    14. Built-In Variables
    15. References
      1. Complex Data Structures
      2. Object-Oriented Programming
      3. Anonymous Subroutines and Closures
    16. Tied Variables
    17. Operator Overloading
    18. Using Other Languages Inside Perl
    19. Embedding
    20. Converting find Command Lines to Perl
    21. Command-Line Options in Your Programs
    22. Embedded Documentation
    23. More Ways to Open Filehandles
    24. Graphical User Interfaces (GUIs)
    25. And More…
  20. C. A Unicode Primer
    1. Unicode
    2. UTF‑8 and Friends
    3. Getting Everyone to Agree
    4. Fancy Characters
      1. Using Unicode in Your Source
      2. Fancier Characters
    5. Dealing with Unicode in Perl
      1. Fancier Characters by Name
      2. Reading from STDIN or Writing to STDOUT or STDERR
      3. Reading from and Writing to Files
      4. Dealing with Command-Line Arguments
      5. Dealing with Databases
    6. Further Reading
  21. D. Experimental Features
    1. A Short History of Perl Development
      1. Perl 5.10 and Beyond
    2. Installing a Recent Perl
    3. Experimental Features
      1. Turning Off Experimental Warnings
      2. Enable or Disable Features Lexically
      3. Don’t Rely on Experimental Features
  22. Index