Cover image for Perl Hacks

Book description

With more than a million dedicated programmers, Perl has proven to be the best computing language for the latest trends in computing and business. While other languages have stagnated, Perl remains fresh, thanks to its community-based development model, which encourages the sharing of information among users. This tradition of knowledge-sharing allows developers to find answers to almost any Perl question they can dream up.

And you can find many of those answers right here in Perl Hacks. Like all books in O'Reilly's Hacks Series, Perl Hacks appeals to a variety of programmers, whether you're an experienced developer or a dabbler who simply enjoys exploring technology. Each hack is a short lesson--some are practical exercises that teach you essential skills, while others merely illustrate some of the fun things that Perl can do. Most hacks have two parts: a direct answer to the immediate problem you need to solve right now and a deeper, subtler technique that you can adapt to other situations. Learn how to add CPAN shortcuts to the Firefox web browser, read files backwards, write graphical games in Perl, and much more.

For your convenience, Perl Hacks is divided by topic--not according to any sense of relative difficulty--so you can skip around and stop at any hack you like. Chapters include:

  • Productivity Hacks

  • User Interaction

  • Data Munging

  • Working with Modules

  • Object Hacks

  • Debugging

Whether you're a newcomer or an expert, you'll find great value in Perl Hacks, the only Perl guide that offers something useful and fun for everyone.

Table of Contents

  1. Perl Hacks
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Credits
      1. About the Authors
      2. About the Contributors
      3. Acknowledgments
        1. chromatic
        2. Damian Conway
        3. Curtis "Ovid" Poe
        4. Technical Reviewers
    4. Preface
      1. Why Perl Hacks?
      2. How To Use This Book
      3. How This Book Is Organized
      4. Conventions Used in This Book
      5. Using Code Examples
      6. Safari Enabled
      7. We'd Like to Hear from You
    5. 1. Productivity Hacks
      1. Hack #1. Add CPAN Shortcuts to Firefox
        1. Search for a Module
        2. Read Module Documentation
        3. Find Module Comments
        4. Hacking the Hack
      2. Hack #2. Put Perldoc to Work
        1. Find Operator Documentation
        2. Answer a FAQ
        3. Webify It
        4. Find that Module!
        5. Browse the Code
      3. Hack #3. Browse Perl Docs Online
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      4. Hack #4. Make the Most of Shell Aliases
        1. Configuring Your Shell
        2. Useful Shell Aliases
          1. Juggle multiple Perl versions
          2. Juggle multiple module versions
          3. Don't forget multipart commands
          4. Remember configuration options
          5. Find a module's version
          6. Change Unix paths to Windows paths and back
      5. Hack #5. Autocomplete Perl Identifiers in Vim
        1. The Hack
        2. Finding Identifiers Automatically
        3. Hacking the Hack
      6. Hack #6. Use the Best Emacs Mode for Perl
        1. The Hack
          1. Put Perldoc at your fingertips
          2. Use a special mode just for Pod
      7. Hack #7. Enforce Local Style
        1. The Hack
          1. From the command line
          2. Within Vim
          3. Within Emacs
      8. Hack #8. Don't Save Bad Perl
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      9. Hack #9. Automate Checkin Code Reviews
        1. The Hack
        2. Hacking the Hack
      10. Hack #10. Run Tests from Within Vim
        1. The Hack
          1. Binding keys
          2. Seeing failures
          3. Managing paths
      11. Hack #11. Run Perl from Emacs
        1. The Hack
    6. 2. User Interaction
      1. Hack #12. Use $EDITOR As Your UI
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      2. Hack #13. Interact Correctly on the Command Line
        1. The Hack
        2. The Hack
      3. Hack #14. Simplify Your Terminal Interactions
        1. Train -req
        2. Yea or Nay
        3. At the Touch of a Button
        4. Engage Cloaking Device
        5. What's On the Menu?
      4. Hack #15. Alert Your Mac
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      5. Hack #16. Interactive Graphical Apps
        1. Blitting with SDL Perl
        2. Animating with SDL Perl
        3. A Working Animation
      6. Hack #17. Collect Configuration Information
        1. The Hack
        2. Using the Hack
      7. Hack #18. Rewrite the Web
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
    7. 3. Data Munging
      1. Hack #19. Treat a File As an Array
        1. The Hack
        2. Running the Hack
      2. Hack #20. Read Files Backwards
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      3. Hack #21. Use Any Spreadsheet As a Data Source
        1. Accessing Cell Data
          1. Do I need Spreadsheet::Read for that?
          2. Accessing a data column
          3. Accessing a row of data
          4. Using programmer-style indexing
          5. Showing all data in a spreadsheet
        2. How It Works
        3. Hacking the Hack
      4. Hack #22. Factor Out Database Code
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      5. Hack #23. Build a SQL Library
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      6. Hack #24. Query Databases Dynamically Without SQL
        1. The Hack
        2. Hacking the Hack
      7. Hack #25. Bind Database Columns
        1. The Hack
        2. Running the Hack
      8. Hack #26. Iterate and Generate Expensive Data
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      9. Hack #27. Pull Multiple Values from an Iterator
        1. Better Context than wantarray( )
        2. The Code
        3. Hacking the Hack
    8. 4. Working with Modules
      1. Hack #28. Shorten Long Class Names
        1. The Hack
          1. Resolve conflicts
          2. Importing with aliased
      2. Hack #29. Manage Module Paths
        1. Within Your Program
        2. From the Command Line
        3. With an Environment Variable
        4. When Recompiling Perl
      3. Hack #30. Reload Modified Modules
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      4. Hack #31. Create Personal Module Bundles
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      5. Hack #32. Manage Module Installations
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      6. Hack #33. Presolve Module Paths
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      7. Hack #34. Create a Standard Module Toolkit
        1. A Mess of Modules
        2. The Hack
        3. Hacking the Hack
      8. Hack #35. Write Demos from Tutorials
        1. The Hack
        2. Running the Hack
        3. Inside the Hack
        4. Hacking the Hack
      9. Hack #36. Replace Bad Code from the Outside
        1. The Hack
        2. Hacking the Hack
      10. Hack #37. Drink to the CPAN
        1. Running the Hack
        2. Hacking the Hack
      11. Hack #38. Improve Exceptional Conditions
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      12. Hack #39. Search CPAN Modules Locally
        1. The Hack
        2. Hacking the Hack
      13. Hack #40. Package Standalone Perl Applications
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      14. Hack #41. Create Your Own Lexical Warnings
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      15. Hack #42. Find and Report Module Bugs
        1. Write a Test
          1. Submitting a bug report
        2. Hacking the Hack
    9. 5. Object Hacks
      1. Hack #43. Turn Your Objects Inside Out
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      2. Hack #44. Serialize Objects (Mostly) for Free
        1. The Hack
      3. Hack #45. Add Information with Attributes
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      4. Hack #46. Make Methods Really Private
        1. The Hack
        2. Running the Hack
          1. Inside the hack
        3. Hacking the Hack
      5. Hack #47. Autodeclare Method Arguments
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      6. Hack #48. Control Access to Remote Objects
        1. The Hack
        2. Hacking the Hack
      7. Hack #49. Make Your Objects Truly Polymorphic
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      8. Hack #50. Autogenerate Your Accessors
        1. The Hack
          1. Class::MethodMaker
          2. Class::BuildMethods
        2. Running the Hack
    10. 6. Debugging
      1. Hack #51. Find Compilation Errors Fast
        1. The Hack
          1. Binary searching for bugs
        2. Hacking the Hack
      2. Hack #52. Make Invisible Characters Apparent
        1. Bracket Your Variables
        2. Bracket Interpolated Lists
        3. Serialize Your Data
      3. Hack #53. Debug with Test Cases
        1. The Hack
          1. Writing test code
          2. From test code to test cases
        2. Hacking the Hack
      4. Hack #54. Debug with Comments
        1. The Hack
          1. Displaying variables
          2. Making Assertions
          3. Configuring smartness levels
      5. Hack #55. Show Source Code on Errors
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      6. Hack #56. Deparse Anonymous Functions
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      7. Hack #57. Name Your Anonymous Subroutines
        1. The Hack
        2. Hacking the Hack
      8. Hack #58. Find a Subroutine's Source
        1. The Hack
        2. Running the Hack
      9. Hack #59. Customize the Debugger
        1. The Hack
          1. Overriding a debugger command
        2. Running the Hack
        3. Hacking the Hack
    11. 7. Developer Tricks
      1. Hack #60. Rebuild Your Distributions
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      2. Hack #61. Test with Specifications
        1. The Hack
        2. Running the Hack
          1. Refining your claims
          2. Interpreting test output
          3. Taking advantage of specification-based testing
      3. Hack #62. Segregate Developer and User Tests
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      4. Hack #63. Run Tests Automatically
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      5. Hack #64. See Test Failure Diagnostics — in Color!
        1. The Hack
        2. Hacking the Hack
      6. Hack #65. Test Live Code
        1. The Hack
        2. Hacking the Hack
      7. Hack #66. Cheat on Benchmarks
      8. Hack #67. Build Your Own Perl
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      9. Hack #68. Run Test Suites Persistently
        1. The Hack
          1. Creating a wrapper around prove
        2. Running the Hack
        3. Hacking the Hack
      10. Hack #69. Simulate Hostile Environments in Your Tests
        1. The Hack
          1. Simulating old versions of modules
          2. Simulating missing modules
          3. Running multiple scenarios
        2. Hacking the Hack
    12. 8. Know Thy Code
      1. Hack #70. Understand What Happens When
        1. The Hack
          1. Compilation
          2. Initialization
          3. Runtime
          4. Cleanup
      2. Hack #71. Inspect Your Data Structures
        1. Dumping References Outside the Debugger
        2. Printing to a File
        3. Output in CGI or mod_perl Programs
      3. Hack #72. Find Functions Safely
        1. The Hack
          1. Get cozy with can( )
          2. Find functions, not methods!
      4. Hack #73. Know What's Core and When
        1. The Hack
          1. Checking by version
      5. Hack #74. Trace All Used Modules
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      6. Hack #75. Find All Symbols in a Package
        1. The Hack
          1. How does it work?
        2. Running the Hack
      7. Hack #76. Peek Inside Closures
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      8. Hack #77. Find All Global Variables
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      9. Hack #78. Introspect Your Subroutines
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      10. Hack #79. Find Imported Functions
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      11. Hack #80. Profile Your Program Size
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      12. Hack #81. Reuse Perl Processes
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      13. Hack #82. Trace Your Ops
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      14. Hack #83. Write Your Own Warnings
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
    13. 9. Expand Your Perl Foo
      1. Hack #84. Double Your Data with Dualvars
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      2. Hack #85. Replace Soft References with Real Ones
        1. The Hack
        2. Hacking the Hack
      3. Hack #86. Optimize Away the Annoying Stuff
        1. The Hack
        2. Hacking the Hack
      4. Hack #87. Lock Down Your Hashes
        1. The Hack
        2. Running the Hack
      5. Hack #88. Clean Up at the End of a Scope
        1. The Hack
        2. Hacking the Hack
      6. Hack #89. Invoke Functions in Odd Ways
        1. The Hack
          1. Make a Bareword invoke a function
          2. Tie a scalar variable to a function
          3. Tie an array variable to a function
          4. Tie a hash variable to a function
          5. Add a function-calling layer to filehandles
      7. Hack #90. Glob Those Sequences
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      8. Hack #91. Write Less Error-Checking Code
        1. The Hack
        2. Hacking the Hack
      9. Hack #92. Return Smarter Values
        1. Fine Distinctions
        2. Name that Return Value
        3. Out, Out, Damn Commas!
      10. Hack #93. Return Active Values
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      11. Hack #94. Add Your Own Perl Syntax
        1. The Hack
      12. Hack #95. Modify Semantics with a Source Filter
        1. The Hack
        2. Hacking the Hack
      13. Hack #96. Use Shared Libraries Without XS
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      14. Hack #97. Run Two Services on a Single TCP Port
        1. The Hack
        2. Running the Hack
        3. Hacking the Hack
      15. Hack #98. Improve Your Dispatch Tables
        1. The hack
        2. Running the Code
      16. Hack #99. Track Your Approximations
        1. Interval interlude
        2. Teaching Perl to think in intervals
      17. Hack #100. Overload Your Operators
        1. The Hack
          1. Saying what you mean
      18. Hack #101. Learn from Obfuscations
    14. Index
    15. About the Authors
    16. Colophon
    17. SPECIAL OFFER: Upgrade this ebook with O’Reilly