Intermediate Perl

Book description

Perl is a versatile, powerful programming language used in a variety of disciplines, ranging from system administration to web programming to database manipulation. One slogan of Perl is that it makes easy things easy and hard things possible. Intermediate Perl is about making the leap from the easy things to the hard ones.

Originally released in 2003 as Learning Perl Objects, References, and Modules and revised and updated for Perl 5.8, this book offers a gentle but thorough introduction to intermediate programming in Perl. Written by the authors of the best-selling Learning Perl, it picks up where that book left off. Topics include:

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

Following the successful format of Learning Perl, we designed each chapter in the book to be small enough to be read in just an hour or two, ending with a series of exercises to help you practice what you've learned. To use the book, you just need to be familiar with the material in Learning Perl and have ambition to go further.

Perl is a different language to different people. It is a quick scripting tool for some, and a fully-featured object-oriented language for others. It is used for everything from performing quick global replacements on text files, to crunching huge, complex sets of scientific data that take weeks to process. Perl is what you make of it. But regardless of what you use Perl for, this book helps you do it more effectively, efficiently, and elegantly.

Intermediate Perl is about learning to use Perl as a programming language, and not just a scripting language. This is the book that turns the Perl dabbler into the Perl programmer.

Publisher resources

View/Submit Errata

Table of contents

  1. A Note Regarding Supplemental Files
  2. Foreword
  3. Preface
    1. Structure of This Book
    2. Conventions Used in This Book
    3. Using Code Examples
    4. Comments and Questions
    5. Safari® Enabled
    6. Acknowledgments
  4. 1. Introduction
    1. 1.1. What Should You Know Already?
    2. 1.2. What About All Those Footnotes?
    3. 1.3. What’s with the Exercises?
    4. 1.4. What If I’m a Perl Course Instructor?
  5. 2. Intermediate Foundations
    1. 2.1. List Operators
      1. 2.1.1. List Filtering with grep
      2. 2.1.2. Transforming Lists with map
    2. 2.2. Trapping Errors with eval
    3. 2.3. Dynamic Code with eval
    4. 2.4. Exercises
      1. 2.4.1. Exercise 1 [15 min]
      2. 2.4.2. Exercise 2 [25 min]
  6. 3. Using Modules
    1. 3.1. The Standard Distribution
    2. 3.2. Using Modules
    3. 3.3. Functional Interfaces
    4. 3.4. Selecting What to Import
    5. 3.5. Object-Oriented Interfaces
    6. 3.6. A More Typical Object-Oriented Module: Math::BigInt
    7. 3.7. The Comprehensive Perl Archive Network
    8. 3.8. Installing Modules from CPAN
    9. 3.9. Setting the Path at the Right Time
      1. 3.9.1. Handling Module Dependencies
    10. 3.10. Exercises
      1. 3.10.1. Exercise 1 [25 min]
      2. 3.10.2. Exercise 2 [35 min]
  7. 4. Introduction to References
    1. 4.1. Performing the Same Task on Many Arrays
    2. 4.2. Taking a Reference to an Array
    3. 4.3. Dereferencing the Array Reference
    4. 4.4. Getting Our Braces Off
    5. 4.5. Modifying the Array
    6. 4.6. Nested Data Structures
    7. 4.7. Simplifying Nested Element References with Arrows
    8. 4.8. References to Hashes
    9. 4.9. Exercises
      1. 4.9.1. Exercise 1 [5 min]
      2. 4.9.2. Exercise 2 [30 min]
  8. 5. References and Scoping
    1. 5.1. More Than One Reference to Data
    2. 5.2. What If That Was the Name?
    3. 5.3. Reference Counting and Nested Data Structures
    4. 5.4. When Reference Counting Goes Bad
    5. 5.5. Creating an Anonymous Array Directly
    6. 5.6. Creating an Anonymous Hash
    7. 5.7. Autovivification
    8. 5.8. Autovivification and Hashes
    9. 5.9. Exercises
      1. 5.9.1. Exercise 1 [5 min]
      2. 5.9.2. Exercise 2 [40 min]
  9. 6. Manipulating Complex Data Structures
    1. 6.1. Using the Debugger to View Complex Data
    2. 6.2. Viewing Complex Data with Data::Dumper
    3. 6.3. YAML
    4. 6.4. Storing Complex Data with Storable
    5. 6.5. Using the map and grep Operators
    6. 6.6. Applying a Bit of Indirection
    7. 6.7. Selecting and Altering Complex Data
    8. 6.8. Exercises
      1. 6.8.1. Exercise 1 [20 min]
      2. 6.8.2. Exercise 2 [5 min]
  10. 7. Subroutine References
    1. 7.1. Referencing a Named Subroutine
    2. 7.2. Anonymous Subroutines
    3. 7.3. Callbacks
    4. 7.4. Closures
    5. 7.5. Returning a Subroutine from a Subroutine
    6. 7.6. Closure Variables as Inputs
    7. 7.7. Closure Variables as Static Local Variables
    8. 7.8. Exercise
      1. 7.8.1. Exercise [50 min]
  11. 8. Filehandle References
    1. 8.1. The Old Way
    2. 8.2. The Improved Way
    3. 8.3. The Even Better Way
    4. 8.4. IO::Handle
      1. 8.4.1. IO::File
      2. 8.4.2. Anonymous IO::File Objects
      3. 8.4.3. IO::Scalar
      4. 8.4.4. IO::Tee
    5. 8.5. Directory Handle References
      1. 8.5.1. IO::Dir
    6. 8.6. Exercises
      1. 8.6.1. Exercise 1 [20 min]
      2. 8.6.2. Exercise 2 [30 min]
      3. 8.6.3. Exercise 3 [15 min]
  12. 9. Practical Reference Tricks
    1. 9.1. Review of Sorting
    2. 9.2. Sorting with Indices
    3. 9.3. Sorting Efficiently
    4. 9.4. The Schwartzian Transform
    5. 9.5. Multi-Level Sort with the Schwartzian Transform
    6. 9.6. Recursively Defined Data
    7. 9.7. Building Recursively Defined Data
    8. 9.8. Displaying Recursively Defined Data
    9. 9.9. Exercises
      1. 9.9.1. Exercise 1 [15 min]
      2. 9.9.2. Exercise 2 [15 min]
      3. 9.9.3. Exercise 3 [10 min]
      4. 9.9.4. Exercise 4 [20 min]
  13. 10. Building Larger Programs
    1. 10.1. The Cure for the Common Code
    2. 10.2. Inserting Code with eval
    3. 10.3. Using do
    4. 10.4. Using require
    5. 10.5. require and @INC
      1. 10.5.1. Extending @INC
      2. 10.5.2. Extending @INC with PERL5LIB
      3. 10.5.3. Extending @INC with -I
    6. 10.6. The Problem of Namespace Collisions
    7. 10.7. Packages as Namespace Separators
    8. 10.8. Scope of a Package Directive
    9. 10.9. Packages and Lexicals
    10. 10.10. Exercises
      1. 10.10.1. Exercise 1 [25 min]
      2. 10.10.2. Exercise 2 [15 min]
  14. 11. Introduction to Objects
    1. 11.1. If We Could Talk to the Animals...
    2. 11.2. Introducing the Method Invocation Arrow
    3. 11.3. The Extra Parameter of Method Invocation
    4. 11.4. Calling a Second Method to Simplify Things
    5. 11.5. A Few Notes About @ISA
    6. 11.6. Overriding the Methods
    7. 11.7. Starting the Search from a Different Place
    8. 11.8. The SUPER Way of Doing Things
    9. 11.9. What to Do with @_
    10. 11.10. Where We Are So Far...
    11. 11.11. Exercises
      1. 11.11.1. Exercise 1 [20 min]
      2. 11.11.2. Exercise 2 [40 min]
  15. 12. Objects with Data
    1. 12.1. A Horse Is a Horse, of Course of Course—or Is It?
    2. 12.2. Invoking an Instance Method
    3. 12.3. Accessing the Instance Data
    4. 12.4. How to Build a Horse
    5. 12.5. Inheriting the Constructor
    6. 12.6. Making a Method Work with Either Classes or Instances
    7. 12.7. Adding Parameters to a Method
    8. 12.8. More Interesting Instances
    9. 12.9. A Horse of a Different Color
    10. 12.10. Getting Our Deposit Back
    11. 12.11. Don’t Look Inside the Box
    12. 12.12. Faster Getters and Setters
    13. 12.13. Getters That Double as Setters
    14. 12.14. Restricting a Method to Class-Only or Instance-Only
    15. 12.15. Exercise
      1. 12.15.1. Exercise [45 min]
  16. 13. Object Destruction
    1. 13.1. Cleaning Up After Yourself
    2. 13.2. Nested Object Destruction
    3. 13.3. Beating a Dead Horse
    4. 13.4. Indirect Object Notation
    5. 13.5. Additional Instance Variables in Subclasses
    6. 13.6. Using Class Variables
    7. 13.7. Weakening the Argument
    8. 13.8. Exercise
      1. 13.8.1. Exercise [45 min]
  17. 14. Some Advanced Object Topics
    1. 14.1. UNIVERSAL Methods
    2. 14.2. Testing Our Objects for Good Behavior
    3. 14.3. AUTOLOAD as a Last Resort
    4. 14.4. Using AUTOLOAD for Accessors
    5. 14.5. Creating Getters and Setters More Easily
    6. 14.6. Multiple Inheritance
    7. 14.7. Exercises
      1. 14.7.1. Exercise 1 [20 min]
      2. 14.7.2. Exercise 2 [40 min]
  18. 15. Exporter
    1. 15.1. What use Is Doing
    2. 15.2. Importing with Exporter
    3. 15.3. @EXPORT and @EXPORT_OK
    4. 15.4. %EXPORT_TAGS
    5. 15.5. Exporting in a Primarily OO Module
    6. 15.6. Custom Import Routines
    7. 15.7. Exercises
      1. 15.7.1. Exercise 1 [15 min]
      2. 15.7.2. Exercise 2 [15 min]
  19. 16. Writing a Distribution
    1. 16.1. There’s More Than One Way To Do It
    2. 16.2. Using h2xs
      1. 16.2.1. MANIFEST
      2. 16.2.2. README
      3. 16.2.3. Changes
      4. 16.2.4. META.yml
      5. 16.2.5. The Prototype Module Itself
    3. 16.3. Embedded Documentation
    4. 16.4. Controlling the Distribution with Makefile.PL
    5. 16.5. Alternate Installation Locations (PREFIX=...)
    6. 16.6. Trivial make test
    7. 16.7. Trivial make install
    8. 16.8. Trivial make dist
    9. 16.9. Using the Alternate Library Location
    10. 16.10. Exercise
      1. 16.10.1. Exercise [30 min]
  20. 17. Essential Testing
    1. 17.1. More Tests Mean Better Code
    2. 17.2. A Simple Test Script
    3. 17.3. The Art of Testing
    4. 17.4. The Test Harness
    5. 17.5. Writing Tests with Test::More
    6. 17.6. Testing Object-Oriented Features
    7. 17.7. A Testing To-Do List
    8. 17.8. Skipping Tests
    9. 17.9. More Complex Tests (Multiple Test Scripts)
    10. 17.10. Exercise
      1. 17.10.1. Exercise [60 min]
  21. 18. Advanced Testing
    1. 18.1. Testing Large Strings
    2. 18.2. Testing Files
    3. 18.3. Testing STDOUT or STDERR
    4. 18.4. Using Mock Objects
    5. 18.5. Testing POD
    6. 18.6. Coverage Testing
    7. 18.7. Writing Your Own Test::* Modules
    8. 18.8. Exercises
      1. 18.8.1. Exercise 1 [20 min]
      2. 18.8.2. Exercise 2 [20 min]
  22. 19. Contributing to CPAN
    1. 19.1. The Comprehensive Perl Archive Network
    2. 19.2. Getting Prepared
    3. 19.3. Preparing Your Distribution
    4. 19.4. Uploading Your Distribution
    5. 19.5. Announcing the Module
    6. 19.6. Testing on Multiple Platforms
    7. 19.7. Consider Writing an Article or Giving a Talk
    8. 19.8. Exercise
      1. 19.8.1. Exercise [Infinite min]
  23. A. Answers to Exercises
    1. A.1. Answers for Chapter 2
      1. A.1.1. Exercise 1
      2. A.1.2. Exercise 2
    2. A.2. Answers for Chapter 3
      1. A.2.1. Exercise 1
      2. A.2.2. Exercise 2
    3. A.3. Answers for Chapter 4
      1. A.3.1. Exercise 1
      2. A.3.2. Exercise 2
    4. A.4. Answers for Chapter 5
      1. A.4.1. Exercise 1
      2. A.4.2. Exercise 2
    5. A.5. Answers for Chapter 6
      1. A.5.1. Exercise 1
      2. A.5.2. Exercise 2
    6. A.6. Answer for Chapter 7
      1. A.6.1. Exercise
    7. A.7. Answers for Chapter 8
      1. A.7.1. Exercise 1
      2. A.7.2. Exercise 2
      3. A.7.3. Exercise 3
    8. A.8. Answers for Chapter 9
      1. A.8.1. Exercise 1
      2. A.8.2. Exercise 2
      3. A.8.3. Exercise 3
      4. A.8.4. Exercise 4
    9. A.9. Answers for Chapter 10
      1. A.9.1. Exercise 1
      2. A.9.2. Exercise 2
    10. A.10. Answers for Chapter 11
      1. A.10.1. Exercise 1
      2. A.10.2. Exercise 2
    11. A.11. Answer for Chapter 12
      1. A.11.1. Exercise
    12. A.12. Answer for Chapter 13
      1. A.12.1. Exercise
    13. A.13. Answers for Chapter 14
      1. A.13.1. Exercise 1
      2. A.13.2. Exercise 2
    14. A.14. Answers for Chapter 15
      1. A.14.1. Exercise 1
      2. A.14.2. Exercise 2
    15. A.15. Answer for Chapter 16
      1. A.15.1. Exercise
    16. A.16. Answer for Chapter 17
      1. A.16.1. Exercise
    17. A.17. Answers for Chapter 18
      1. A.17.1. Exercise 1
      2. A.17.2. Exercise 2
    18. A.18. Answer for Chapter 19
      1. A.18.1. Exercise
  24. Index
  25. About the Authors
  26. Colophon
  27. Copyright

Product information

  • Title: Intermediate Perl
  • Author(s): Randal L. Schwartz, brian d foy, Tom Phoenix
  • Release date: March 2006
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596102067