0%

If you're just getting started with Perl, this is the book you want—whether you're a programmer, system administrator, or web hacker. Nicknamed "the Llama" by two generations of users, this best seller closely follows the popular introductory Perl course taught by the authors since 1991. This eighth edition covers recent changes to the language up to version 5.34.

Perl is suitable for almost any task on almost any platform, from short fixes to complete web applications. Learning Perl teaches you the basics and shows you how to write simple, single-file programs—roughly 90% of the Perl programs in use today. And each chapter includes exercises to help you practice what you've just learned. Other books may teach you to program in Perl, but this book will turn you into a Perl programmer.

Topics include:

  • Perl data and variable types
  • Subroutines
  • File operations
  • Regular expressions
  • String manipulation (including Unicode)
  • Lists and sorting
  • Process management
  • Use of third-party modules

Table of Contents

  1. Preface
    1. Typographical Conventions
    2. Code Examples
    3. O’Reilly Online Learning
    4. How to Contact Us
    5. History of This Book
    6. Changes from the Previous Edition
    7. Acknowledgments
    8. From Randal
    9. From brian
    10. From Tom
    11. From All of Us
  2. 1. Introduction
    1. Questions and Answers
    2. Is This the Right Book for You?
    3. What About the Exercises and Their Answers?
    4. What If I’m a Perl Course Instructor?
    5. What Does “Perl” Stand For?
    6. Why Did Larry Create Perl?
    7. Why Didn’t Larry Just Use Some Other Language?
    8. Is Perl Easy or Hard?
    9. How Did Perl Get to Be So Popular?
    10. What’s Happening with Perl Now?
    11. What’s Perl Really Good For?
    12. What Is Perl Not Good For?
    13. How Can I Get Perl?
    14. What Is CPAN?
    15. Is There Any Kind of Support?
    16. What If I Find a Bug in Perl?
    17. How Do I Make a Perl Program?
    18. A Simple Program
    19. What’s Inside That Program?
    20. How Do I Compile My Perl Program?
    21. A Whirlwind Tour of Perl
    22. Exercises
  3. 2. Scalar Data
    1. Numbers
    2. All Numbers Have the Same Format Internally
    3. Integer Literals
    4. Nondecimal Integer Literals
    5. Floating-Point Literals
    6. Numeric Operators
    7. Strings
    8. Single-Quoted String Literals
    9. Double-Quoted String Literals
    10. String Operators
    11. Automatic Conversion Between Numbers and Strings
    12. Perl’s Built-in Warnings
    13. Interpreting Nondecimal Numerals
    14. Scalar Variables
    15. Choosing Good Variable Names
    16. Scalar Assignment
    17. Compound Assignment Operators
    18. Output with print
    19. Interpolation of Scalar Variables into Strings
    20. Creating Characters by Code Point
    21. Operator Precedence and Associativity
    22. Comparison Operators
    23. The if Control Structure
    24. Boolean Values
    25. Getting User Input
    26. The chomp Operator
    27. The while Control Structure
    28. The undef Value
    29. The defined Function
    30. Exercises
  4. 3. Lists and Arrays
    1. Accessing Elements of an Array
    2. Special Array Indices
    3. List Literals
    4. The qw Shortcut
    5. List Assignment
    6. The pop and push Operators
    7. The shift and unshift Operators
    8. The splice Operator
    9. Interpolating Arrays into Strings
    10. The foreach Control Structure
    11. Perl’s Favorite Default: $_
    12. The reverse Operator
    13. The sort Operator
    14. The each Operator
    15. Scalar and List Context
    16. Using List-Producing Expressions in Scalar Context
    17. Using Scalar-Producing Expressions in List Context
    18. Forcing Scalar Context
    19. <STDIN> in List Context
    20. 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
    7. A Better &max Routine
    8. Empty Parameter Lists
    9. Notes on Lexical (my) Variables
    10. The use strict Pragma
    11. The return Operator
    12. Omitting the Ampersand
    13. Nonscalar Return Values
    14. Persistent, Private Variables
    15. Subroutine Signatures
    16. Prototypes
    17. Exercises
  6. 5. Input and Output
    1. Input from Standard Input
    2. Input from the Diamond Operator
    3. The Double Diamond
    4. The Invocation Arguments
    5. Output to Standard Output
    6. Formatted Output with printf
    7. Arrays and printf
    8. Filehandles
    9. Opening a Filehandle
    10. Binmoding Filehandles
    11. Bad Filehandles
    12. Closing a Filehandle
    13. Fatal Errors with die
    14. Warning Messages with warn
    15. Automatically die-ing
    16. Using Filehandles
    17. Changing the Default Output Filehandle
    18. Reopening a Standard Filehandle
    19. Output with say
    20. Filehandles in a Scalar
    21. Exercises
  7. 6. Hashes
    1. What Is a Hash?
    2. Why Use a Hash?
    3. Hash Element Access
    4. The Hash as a Whole
    5. Hash Assignment
    6. The Big Arrow
    7. Hash Functions
    8. The keys and values Functions
    9. The each Function
    10. Typical Use of a Hash
    11. The exists Function
    12. The delete Function
    13. Hash Element Interpolation
    14. The %ENV Hash
    15. Exercises
  8. 7. Regular Expressions
    1. Sequences
    2. Practice Some Patterns
    3. The Wildcard
    4. Quantifiers
    5. Grouping in Patterns
    6. Alternation
    7. Character Classes
    8. Character Class Shortcuts
    9. Negating the Shortcuts
    10. Unicode Properties
    11. Anchors
    12. Word Anchors
    13. Exercises
  9. 8. Matching with Regular Expressions
    1. Matches with m//
    2. Match Modifiers
    3. Case-Insensitive Matching with /i
    4. Matching Any Character with /s
    5. Adding Whitespace with /x
    6. Combining Option Modifiers
    7. Choosing a Character Interpretation
    8. Beginning- and End-of-Line Anchors
    9. Other Options
    10. The Binding Operator =~
    11. The Match Variables
    12. The Persistence of Captures
    13. Captures in Alternations
    14. Noncapturing Parentheses
    15. Named Captures
    16. The Automatic Match Variables
    17. Precedence
    18. Examples of Precedence
    19. And There’s More
    20. A Pattern Test Program
    21. Exercises
  10. 9. Processing Text with Regular Expressions
    1. Substitutions with s///
    2. Global Replacements with /g
    3. Different Delimiters
    4. Substitution Modifiers
    5. The Binding Operator
    6. Nondestructive Substitutions
    7. Case Shifting
    8. Metaquoting
    9. The split Operator
    10. The join Function
    11. m// in List Context
    12. More Powerful Regular Expressions
    13. Nongreedy Quantifiers
    14. Fancier Word Boundaries
    15. Matching Multiple-Line Text
    16. Updating Many Files
    17. In-Place Editing from the Command Line
    18. Exercises
  11. 10. More Control Structures
    1. The unless Control Structure
    2. The else Clause with unless
    3. The until Control Structure
    4. Statement Modifiers
    5. The Naked Block Control Structure
    6. The elsif Clause
    7. Autoincrement and Autodecrement
    8. The Value of Autoincrement
    9. The for Control Structure
    10. The Secret Connection Between foreach and for
    11. Loop Controls
    12. The last Operator
    13. The next Operator
    14. The redo Operator
    15. Labeled Blocks
    16. The Conditional Operator
    17. Logical Operators
    18. The Value of a Short-Circuit Operator
    19. The defined-or Operator
    20. Control Structures Using Partial-Evaluation Operators
    21. Exercises
  12. 11. Perl Modules
    1. Finding Modules
    2. Installing Modules
    3. Using Your Own Directories
    4. Using Simple Modules
    5. The File::Basename Module
    6. Using Only Some Functions from a Module
    7. The File::Spec Module
    8. Path::Class
    9. Databases and DBI
    10. Dates and Times
    11. Exercises
  13. 12. File Tests
    1. File Test Operators
    2. Testing Several Attributes of the Same File
    3. Stacked File Test Operators
    4. The stat and lstat Functions
    5. The localtime Function
    6. Bitwise Operators
    7. Using Bitstrings
    8. 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
    4. Using sprintf with “Money Numbers”
    5. Advanced Sorting
    6. Sorting a Hash by Value
    7. Sorting by Multiple Keys
    8. Exercises
  16. 15. Process Management
    1. The system Function
    2. Avoiding the Shell
    3. The Environment Variables
    4. The exec Function
    5. Using Backquotes to Capture Output
    6. Using Backquotes in a List Context
    7. External Processes with IPC::System::Simple
    8. Processes as Filehandles
    9. Getting Down and Dirty with fork
    10. Sending and Receiving Signals
    11. Exercises
  17. 16. Some Advanced Perl Techniques
    1. Slices
    2. Array Slice
    3. Hash Slice
    4. Key-Value Slices
    5. Trapping Errors
    6. Using eval
    7. More Advanced Error Handling
    8. Picking Items from a List with grep
    9. Transforming Items from a List with map
    10. Fancier List Utilities
    11. 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
    5. Writing Your Own Modules
    6. Databases
    7. Mathematics
    8. Lists and Arrays
    9. Bits and Pieces
    10. Formats
    11. Networking and IPC
    12. System V IPC
    13. Sockets
    14. Security
    15. Debugging
    16. Command-Line Options
    17. Built-in Variables
    18. References
    19. Complex Data Structures
    20. Object-Oriented Programming
    21. Anonymous Subroutines and Closures
    22. Tied Variables
    23. Operator Overloading
    24. Using Other Languages Inside Perl
    25. Embedding
    26. Converting find Command Lines to Perl
    27. Command-Line Options in Your Programs
    28. Embedded Documentation
    29. More Ways to Open Filehandles
    30. Graphical User Interfaces (GUIs)
    31. And More…
  20. C. A Unicode Primer
    1. Unicode
    2. UTF-8 and Friends
    3. Getting Everyone to Agree
    4. Fancy Characters
    5. Using Unicode in Your Source
    6. Fancier Characters
    7. Dealing with Unicode in Perl
    8. Fancier Characters by Name
    9. Reading from STDIN or Writing to STDOUT or STDERR
    10. Reading from and Writing to Files
    11. Dealing with Command-Line Arguments
    12. Dealing with Databases
    13. Further Reading
  21. D. Experimental Features
    1. A Short History of Perl Development
    2. Perl 5.10 and Beyond
    3. Installing a Recent Perl
    4. Experimental Features
    5. Turning Off Experimental Warnings
    6. Enable or Disable Features Lexically
    7. Don’t Rely on Experimental Features
  22. Index
18.221.208.183