Productivity Hacks
Total Page:16
File Type:pdf, Size:1020Kb
PERL HACKSTM Other Perl resources from O’Reilly Related titles Perl Best Practices Programming Perl Perl Testing: A Devel- Intermediate Perl oper’s Notebook Perl Cookbook Advanced Perl Programming Hacks Series Home hacks.oreilly.com is a community site for developers and power users ofall stripes. Readers learn fromeach other as they share their favorite tips and tools for Mac OS X, Linux, Google, Windows XP, and more. Perl Books perl.oreilly.com is a complete catalog ofO’Reilly’s books Resource Center on Perl and related technologies, including sample chap- ters and code examples. Perl.com is the central web site for the Perl community. It is the perfect starting place for finding out everything there is to know about Perl. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We special- ize in documenting the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online reference library for programmers and IT professionals. Conduct searches across more than 1,000 books. Sub- scribers can zero in on answers to time-critical questions in a matter ofseconds. Read the books on your Book- shelf from cover to cover or simply flip to the page you need. Try it today for free. PERL HACKSTM chromatic with Damian Conway and Curtis "Ovid" Poe Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo Perl Hacks™ by chromatic with Damian Conway and Curtis “Ovid” Poe Copyright © 2006 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected]. Editors: Allison Randal and Tatiana Apandi Indexer: Johnna VanHoose Dinse Production Editor: Darren Kelly Cover Designer: Linda Palo Copyeditor: Nancy Reinhardt Interior Designer: David Futato Proofreader: Darren Kelly Illustrators: Robert Romano and Jessamyn Read Printing History: May 2006: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks ofO’Reilly Media, Inc. The Hacks series designations, Perl Hacks, the image ofa pair ofwork boots, and related trade dress are trademarks of O’Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. Small print: The technologies discussed in this publication, the limitations on these technologies that technology and content owners seek to impose, and the laws actually limiting the use ofthese technologies are constantly changing. Thus, some of the hacks described in this publication may not work, may cause unintended harm to systems on which they are used, or may not be consistent with applicable user agreements. Your use ofthese hacks is at your own risk, and O’Reilly Media, Inc. disclaims responsibility for any damage or expense resulting from their use. In any event, you should take care that your use of these hacks does not violate any applicable laws, including copyright laws. This book uses RepKover™, a durable and flexible lay-flat binding. ISBN13: 978-0-596-52674-0 [M] [4/08] (-Contents Credits . ix Preface . xv Chapter 1. Productivity Hacks . 1 1. Add CPAN Shortcuts to Firefox 1 2. Put Perldoc to Work 3 3. Browse Perl Docs Online 6 4. Make the Most of Shell Aliases 8 5. Autocomplete Perl Identifiers in Vim 11 6. Use the Best Emacs Mode for Perl 14 7. Enforce Local Style 15 8. Don’t Save Bad Perl 18 9. Automate Checkin Code Reviews 22 10. Run Tests from Within Vim 25 11. Run Perl from Emacs 26 Chapter 2. User Interaction . 28 12. Use $EDITOR As Your UI 28 13. Interact Correctly on the Command Line 30 14. Simplify Your Terminal Interactions 32 15. Alert Your Mac 37 16. Interactive Graphical Apps 39 17. Collect Configuration Information 45 18. Rewrite the Web 48 v Chapter 3. Data Munging . 51 19. Treat a File As an Array 51 20. Read Files Backwards 53 21. Use Any Spreadsheet As a Data Source 54 22. Factor Out Database Code 58 23. Build a SQL Library 61 24. Query Databases Dynamically Without SQL 63 25. Bind Database Columns 64 26. Iterate and Generate Expensive Data 66 27. Pull Multiple Values from an Iterator 68 Chapter 4. Working with Modules . 71 28. Shorten Long Class Names 71 29. Manage Module Paths 72 30. Reload Modified Modules 74 31. Create Personal Module Bundles 76 32. Manage Module Installations 78 33. Presolve Module Paths 81 34. Create a Standard Module Toolkit 83 35. Write Demos from Tutorials 85 36. Replace Bad Code from the Outside 87 37. Drink to the CPAN 89 38. Improve Exceptional Conditions 90 39. Search CPAN Modules Locally 93 40. Package Standalone Perl Applications 96 41. Create Your Own Lexical Warnings 99 42. Find and Report Module Bugs 101 Chapter 5. Object Hacks . 106 43. Turn Your Objects Inside Out 106 44. Serialize Objects (Mostly) for Free 109 45. Add Information with Attributes 111 46. Make Methods Really Private 113 47. Autodeclare Method Arguments 116 48. Control Access to Remote Objects 119 49. Make Your Objects Truly Polymorphic 122 50. Autogenerate Your Accessors 125 vi | Contents Chapter 6. Debugging . 129 51. Find Compilation Errors Fast 129 52. Make Invisible Characters Apparent 130 53. Debug with Test Cases 133 54. Debug with Comments 135 55. Show Source Code on Errors 138 56. Deparse Anonymous Functions 141 57. Name Your Anonymous Subroutines 143 58. Find a Subroutine’s Source 145 59. Customize the Debugger 146 Chapter 7. Developer Tricks . 150 60. Rebuild Your Distributions 150 61. Test with Specifications 151 62. Segregate Developer and User Tests 155 63. Run Tests Automatically 158 64. See Test Failure Diagnostics—in Color! 159 65. Test Live Code 162 66. Cheat on Benchmarks 164 67. Build Your Own Perl 166 68. Run Test Suites Persistently 168 69. Simulate Hostile Environments in Your Tests 173 Chapter 8. Know Thy Code . 178 70. Understand What Happens When 178 71. Inspect Your Data Structures 182 72. Find Functions Safely 184 73. Know What’s Core and When 186 74. Trace All Used Modules 187 75. Find All Symbols in a Package 191 76. Peek Inside Closures 193 77. Find All Global Variables 196 78. Introspect Your Subroutines 198 79. Find Imported Functions 202 80. Profile Your Program Size 204 81. Reuse Perl Processes 206 Contents | vii 82. Trace Your Ops 208 83. Write Your Own Warnings 211 Chapter 9. Expand Your Perl Foo . 215 84. Double Your Data with Dualvars 215 85. Replace Soft References with Real Ones 217 86. Optimize Away the Annoying Stuff 219 87. Lock Down Your Hashes 221 88. Clean Up at the End of a Scope 222 89. Invoke Functions in Odd Ways 224 90. Glob Those Sequences 230 91. Write Less Error-Checking Code 233 92. Return Smarter Values 235 93. Return Active Values 238 94. Add Your Own Perl Syntax 241 95. Modify Semantics with a Source Filter 244 96. Use Shared Libraries Without XS 248 97. Run Two Services on a Single TCP Port 250 98. Improve Your Dispatch Tables 253 99. Track Your Approximations 257 100. Overload Your Operators 260 101. Learn from Obfuscations 264 Index . 267 viii | Contents 0 Credits About the Authors chromatic works for O’Reilly Media, where he edits the free and open source web sites ofthe O’Reilly Network. In his spare time, he writes books such as this one. In the remaining minutes, he contributes to the CPAN, Perl 5, Perl 6, Parrot, and even Pugs. He lives just west ofPortland, Oregon by a park and a creek and would like to finish more projects someday, including writing a novel, a comic book, a television show, and sleeping. Catch up on his hobbies at http://wgz.org/chromatic. Dr. Damian Conway is a professional Perl boffin. As the author of numer- ous popular CPAN modules (http://search.cpan.org/~dconway) and two highly regarded books on Perl, he is also a widely sought-after conference speaker and runs an international IT training company—Thoughtstream (http://damian.conway.org)—which provides Perl training from beginner to masterclass level throughout Europe, North America, and Australasia. He spends most ofhis less-than-copious freetime working with Larry Wall on the design ofPerl 6. [Hack #5], [Hack #13], [Hack #14], [Hack #34], [Hack #54], [Hack #86], [Hack #90], [Hack #92], [Hack #93], [Hack #94], [Hack #95], [Hack #99], [Hack #100] Earlier this century, displaying his usual stellar sense oftiming, Curtis “Ovid” Poe switched from mainframes to web programming in Perl and promptly watched the dot-com industry implode. Despite this minor set- back and working for several currently non-existent companies, Ovid stuck with Perl and actually discovered he liked it. A frequent speaker at user groups and author ofnumerous CPAN modules and a popular Perl CGI course, Ovid is a Perl Foundation Steering Committee member and also heads the TPF grant committee.