Intermediate Perl, 2nd Edition

Book description

This book picks up right where Learning Perl leaves off. With Intermediate Perl, you’ll graduate from short scripts to much larger programs, using features that make Perl a general-purpose language. This gentle but thorough guide introduces you to modules, complex data structures, and object-oriented programming.

Each chapter is small enough to be read in just an hour or two, ending with exercises to help you practice what you’ve learned. If you’re familiar with the material in Learning Perl and have the ambition to go further, Intermediate Perl will teach you most of the core Perl language concepts you need for writing robust programs on any platform.

Topics include:

  • Packages and namespaces
  • References and scoping, including regular expression references
  • Manipulating complex data structures
  • Object-oriented programming
  • Writing and using modules
  • Testing Perl code
  • Contributing to CPAN

Just like Learning Perl, material in this book closely follows the popular introductory Perl course the authors have taught since 1991. This second edition covers recent changes to the language up to version 5.14.

Publisher resources

View/Submit Errata

Table of contents

  1. Intermediate Perl
  2. Foreword
  3. Preface
    1. Structure of This Book
    2. Conventions Used in This Book
    3. Using Code Examples
    4. Safari® Books Online
    5. How to Contact Us
    6. Acknowledgments
  4. 1. Introduction
    1. What Should You Know Already?
    2. strict and warnings
    3. Perl v5.14
      1. A Note on Versions
    4. What About All Those Footnotes?
    5. What’s With the Exercises?
    6. How to Get Help
    7. What If I’m a Perl Course Instructor?
    8. Exercises
  5. 2. Using Modules
    1. The Standard Distribution
    2. Exploring CPAN
    3. Using Modules
    4. Functional Interfaces
      1. Selecting What to Import
    5. Object-Oriented Interfaces
      1. A More Typical Object-Oriented Module: Math::BigInt
      2. Fancier Output with Modules
    6. What’s in Core?
    7. The Comprehensive Perl Archive Network
    8. Installing Modules from CPAN
      1. CPANminus
      2. Installing Modules Manually
    9. Setting the Path at the Right Time
    10. Setting the Path Outside the Program
      1. Extending @INC with PERL5LIB
      2. Extending @INC on the Command Line
    11. local::lib
    12. Exercises
  6. 3. Intermediate Foundations
    1. List Operators
      1. List Filtering with grep
      2. Transforming Lists with map
    2. Trapping Errors with eval
    3. Dynamic Code with eval
    4. The do Block
    5. Exercises
  7. 4. Introduction to References
    1. Doing the Same Task on Many Arrays
    2. PeGS: Perl Graphical Structures
    3. Taking a Reference to an Array
    4. Dereferencing the Array Reference
    5. Getting Our Braces Off
    6. Modifying the Array
    7. Nested Data Structures
    8. Simplifying Nested Element References with Arrows
    9. References to Hashes
    10. Checking Reference Types
    11. Exercises
  8. 5. References and Scoping
    1. More than One Reference to Data
    2. What If That Was the Name?
    3. Reference Counting and Nested Data Structures
    4. When Reference Counting Goes Bad
    5. Creating an Anonymous Array Directly
    6. Creating an Anonymous Hash
    7. Autovivification
    8. Autovivification and Hashes
    9. Exercises
  9. 6. Manipulating Complex Data Structures
    1. Using the Debugger to View Complex Data
    2. Viewing Complex Data with Data::Dumper
      1. Other Dumpers
    3. Marshalling Data
      1. Storing Complex Data with Storable
      2. YAML
      3. JSON
    4. Using the map and grep Operators
    5. Applying a Bit of Indirection
    6. Selecting and Altering Complex Data
    7. Exercises
  10. 7. Subroutine References
    1. Referencing a Named Subroutine
    2. Anonymous Subroutines
    3. Callbacks
    4. Closures
    5. Returning a Subroutine from a Subroutine
    6. Closure Variables as Inputs
    7. Closure Variables as Static Local Variables
      1. state Variables
    8. Finding Out Who We Are
      1. Enchanting Subroutines
      2. Dumping Closures
    9. Exercise
  11. 8. Filehandle References
    1. The Old Way
    2. The Improved Way
    3. Filehandles to Strings
      1. Processing Strings Line by Line
    4. Collections of Filehandles
    5. IO::Handle and Friends
      1. IO::File
      2. IO::Scalar
      3. IO::Tee
      4. IO::Pipe
      5. IO::Null and IO::Interactive
    6. Directory Handles
      1. Directory Handle References
    7. Exercises
  12. 9. Regular Expression References
    1. Before Regular Expression References
    2. Precompiled Patterns
      1. Regular Expression Options
      2. Applying Regex References
    3. Regexes as Scalars
    4. Build Up Regular Expressions
    5. Regex-Creating Modules
      1. Using Common Patterns
      2. Assembling Regular Expressions
    6. Exercises
  13. 10. Practical Reference Tricks
    1. Fancier Sorting
    2. Sorting with Indices
    3. Sorting Efficiently
    4. The Schwartzian Transform
    5. Multilevel Sort with the Schwartzian Transform
    6. Recursively Defined Data
    7. Building Recursively Defined Data
    8. Displaying Recursively Defined Data
    9. Avoiding Recursion
      1. The Breadth-First Solution
    10. Exercises
  14. 11. Building Larger Programs
    1. The Cure for the Common Code
    2. Inserting Code with eval
    3. Using do
    4. Using require
    5. The Problem of Namespace Collisions
    6. Packages as Namespace Separators
    7. Scope of a Package Directive
    8. Packages and Lexicals
    9. Package Blocks
    10. Exercises
  15. 12. Creating Your Own Perl Distribution
    1. Perl’s Two Build Systems
      1. Inside Makefile.PL
      2. Inside Build.PL
    2. Our First Distribution
      1. h2xs
      2. Module::Starter
      3. Custom Templates
    3. Inside Your Perl Distribution
      1. The META File
      2. Adding Additional Modules
    4. Inside a Module
    5. Plain Ol’ Documentation
      1. Pod Command Paragraphs
      2. Pod Paragraphs
      3. Pod Formatting Codes
      4. Checking the Pod Format
    6. The Module Code
    7. Module Building Summary
      1. Creating a Module::Build Distribution
      2. Creating a ExtUtils::Makemaker Distribution
    8. Exercises
  16. 13. Introduction to Objects
    1. If We Could Talk to the Animals. . .
    2. Introducing the Method Invocation Arrow
    3. The Extra Parameter of Method Invocation
    4. Calling a Second Method to Simplify Things
    5. A Few Notes About @ISA
    6. Overriding the Methods
    7. Starting the Search from a Different Place
    8. The SUPER Way of Doing Things
    9. What to Do with @_
    10. Where We Are
    11. Our Barnyard Summary
    12. Exercises
  17. 14. Introduction to Testing
    1. Why Should We Test?
    2. The Perl Testing Process
      1. Test Anywhere Protocol
    3. The Art of Testing
      1. A Test Example
    4. The Test Harness
    5. The Standard Tests
      1. Checking that Modules Compile
      2. The Boilerplate Tests
      3. The Pod Tests
    6. Adding Our First Tests
    7. Measuring Our Test Coverage
      1. Subroutine Coverage
      2. Statement Coverage
      3. Branch Coverage
      4. Conditional Coverage
    8. Exercises
  18. 15. Objects with Data
    1. A Horse Is a Horse, of Course of Course—Or Is It?
    2. Invoking an Instance Method
    3. Accessing the Instance Data
    4. How to Build a Horse
    5. Inheriting the Constructor
    6. Making a Method Work with Either Classes or Instances
    7. Adding Parameters to a Method
    8. More Interesting Instances
    9. A Horse of a Different Color
    10. Getting Our Deposit Back
    11. Don’t Look Inside the Box
    12. Faster Getters and Setters
    13. Getters that Double as Setters
    14. Restricting a Method to Class Only or Instance Only
    15. Exercise
  19. 16. Some Advanced Object Topics
    1. UNIVERSAL Methods
    2. Testing Our Objects for Good Behavior
    3. The Last Resort
    4. Using AUTOLOAD for Accessors
    5. Creating Getters and Setters More Easily
    6. Multiple Inheritance
    7. Exercises
  20. 17. Exporter
    1. What use Is Doing
    2. Importing with Exporter
    3. @EXPORT and @EXPORT_OK
    4. Grouping with %EXPORT_TAGS
    5. Custom Import Routines
    6. Exercises
  21. 18. Object Destruction
    1. Cleaning Up After Ourselves
    2. Nested Object Destruction
    3. Beating a Dead Horse
    4. Indirect Object Notation
    5. Additional Instance Variables in Subclasses
    6. Using Class Variables
    7. Weakening the Argument
    8. Exercise
  22. 19. Introduction to Moose
    1. Making Animals with Moose
      1. Roles Instead of Inheritance
      2. Default Values
      3. Constraining Values
      4. Wrapping Methods
      5. Read-Only Attributes
    2. Improving the Race Horse
    3. Further Study
    4. Exercises
  23. 20. Advanced Testing
    1. Skipping Tests
    2. Testing Object-Oriented Features
    3. Grouping Tests
    4. Testing Large Strings
    5. Testing Files
    6. Testing STDOUT or STDERR
    7. Using Mock Objects
    8. Writing Our Own Test::* Modules
    9. Exercises
  24. 21. Contributing to CPAN
    1. The Comprehensive Perl Archive Network
    2. Getting Prepared
    3. How PAUSE Works
      1. The Indexer
      2. Module Maintainers
    4. Before We Start Work
    5. Preparing the Distribution
      1. Create or Update the README
      2. Check the Build File
      3. Update the Manifest
      4. Increase the Version String
      5. Test the Distribution
    6. Uploading the Distribution
    7. Testing on Multiple Platforms
    8. Announcing the Module
    9. Exercises
  25. A. Answers to Exercises
    1. Answers for Chapter 1
      1. Exercise 1
      2. Exercise 2
    2. Answers for Chapter 2
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
    3. Answers for Chapter 3
      1. Exercise 1
      2. Exercise 2
    4. Answers for Chapter 4
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
    5. Answers for Chapter 5
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
    6. Answers for Chapter 6
      1. Exercise 1
      2. Exercise 2
    7. Answer for Chapter 7
      1. Exercise 1
    8. Answers for Chapter 8
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
    9. Answers for Chapter 9
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
    10. Answers for Chapter 10
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
    11. Answers for Chapter 11
      1. Exercise 1
      2. Exercise 2
    12. Answers for Chapter 12
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
    13. Answers for Chapter 13
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
    14. Answers for Chapter 14
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
    15. Answer for Chapter 15
      1. Exercise 1
    16. Answers for Chapter 16
      1. Exercise 1
      2. Exercise 2
    17. Answers for Chapter 17
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
    18. Answers for Chapter 18
      1. Exercise 1
    19. Answers for Chapter 19
      1. Exercise 1
      2. Exercise 2
    20. Answers for Chapter 20
      1. Exercise 1
      2. Exercise 2
    21. Answers for Chapter 21
      1. Exercise 1
      2. Exercise 2
      3. Exercise 3
      4. Exercise 4
      5. Exercise 5
      6. Exercise 6
      7. Exercise 7
  26. Index of Modules in this Book
  27. Index
  28. About the Authors
  29. Colophon
  30. Copyright

Product information

  • Title: Intermediate Perl, 2nd Edition
  • Author(s): Randal L. Schwartz, brian d foy, Tom Phoenix
  • Release date: July 2012
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449343804