Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc.The Hacks series designations, Perl Hacks, the image of a pair of wo
Trang 1By chromatic , Damian Conway, Curtis "Ovid" Poe
Publisher: O'Reilly Pub Date: May 2006 Print ISBN-10: 0-596-52674-1 Print ISBN-13: 978-0-59-652674-0 Pages: 296
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
Trang 2useful and fun for everyone.
Trang 3By chromatic , Damian Conway, Curtis "Ovid" Poe
Publisher: O'Reilly Pub Date: May 2006 Print ISBN-10: 0-596-52674-1 Print ISBN-13: 978-0-59-652674-0 Pages: 296
Trang 7Printed in the United States of America
Published by O'Reilly Media, Inc., 1005 Gravenstein HighwayNorth, Sebastopol, CA 95472
O'Reilly books may be purchased for educational, business, orsales promotional use Online editions are also available for
most titles (http://safari.oreilly.com) For more information,contact our corporate/institutional sales department: (800)
998-9938 or corporate@oreilly.com
Nutshell Handbook, the Nutshell Handbook logo, and the
O'Reilly logo are registered trademarks of O'Reilly Media, Inc.The Hacks series designations, Perl Hacks, the image of a pair
of work boots, and related trade dress are trademarks of
O'Reilly Media, Inc
Many of the designations used by manufacturers and sellers todistinguish their products are claimed as trademarks Wherethose designations appear in this book, and O'Reilly Media, Inc.was aware of a trademark claim, the designations have beenprinted in caps or initial caps
While every precaution has been taken in the preparation of thisbook, the publisher and authors assume no responsibility forerrors or omissions, or for damages resulting from the use ofthe information contained herein
Small print: The technologies discussed in this publication, thelimitations on these technologies that technology and contentowners seek to impose, and the laws actually limiting the use ofthese technologies are constantly changing Thus, some of thehacks described in this publication may not work, may causeunintended harm to systems on which they are used, or maynot be consistent with applicable user agreements Your use of
Trang 8disclaims responsibility for any damage or expense resultingfrom their use In any event, you should take care that your use
of these hacks does not violate any applicable laws, includingcopyright laws
Trang 9
About the Authors
chromatic works for O'Reilly Media, where he edits the free andopen source web sites of the 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, andeven Pugs He lives just west of Portland, 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, andsleeping Catch up on his hobbies at http://wgz.org/chromatic
Dr Damian Conway is a professional Perl boffin As the author
of numerous popular CPAN modules
(http://search.cpan.org/~dconway) and two highly regardedbooks on Perl, he is also a widely sought-after conference
speaker and runs an international IT training
companyThoughtstream (http://damian.conway.org)which
provides Perl training from beginner to masterclass level
throughout Europe, North America, and Australasia He spendsmost of his less-than-copious free time working with Larry Wall
on the design of Perl 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 of timing,Curtis "Ovid" Poe switched from mainframes to web
programming in Perl and promptly watched the dot-com
industry implode Despite this minor setback and working forseveral currently non-existent companies, Ovid stuck with Perland actually discovered he liked it A frequent speaker at usergroups and author of numerous CPAN modules and a popularPerl CGI course, Ovid is a Perl Foundation Steering Committee
Trang 10About the Contributors
Perl is also a language with a rich and varied community of
experts, wizards, gurus, goofs, and ne'er-do-wells A few of themost colorful characters[1] contributed to this book
[1] Pun strongly intended.
Adrian Howard still hopes that Lisp and Smalltalk will take overthe world, but in the meantime gets paid for playing with Perland Ruby amongst other things Agile fanatic Testing bigot.Usability zealot Recently guilt tripped into being a Perl GrantManager by Ovid Saving up for a small castle to share with hisbeloved Kathryn and a small band of loyal Yorkshire Terriers.[Hack #15], [Hack #63]
Chris Dolan is a software developer living in Madison,
Wisconsin With a Ph.D in Astronomy, he has a very strongmath and science background He started programming
professionally as a teenager in the late 1980s During his freetime, he is an active participant in several online software
development communities and is an avid bicyclist [Hack #40]
David Landgren started using Perl 4.019 when awk was no
longer sufficient to cope with the things he was trying to do In
1999 he founded the Paris Perl Mongers and helped organizethe YAPC::Europe 2003 conference He currently works at alarge French human resources firm, where he likes to go by thetitle of IT Operations Manager, using Perl everywhere He
spends what little spare time he has left writing summaries forthe perl5-porters mailing list Contact him at
david@landgren.net [Hack #98]
Trang 11interested in computers for too long to remember, enjoys
programming with various tools and languages, and found
himself lucky enough to be the catalyst for a game written inPerl called "Frozen-Bubble" which is regularly cited as the
favorite game of the Linux community [Hack #16]
H Merijn Brand is a Perl 5 porter, the Configure pumpking, and
an Amsterdam Perl Monger He was born on December 30,
1961, is married, and has two kids He has worked as a dataanalyst for PROCURA B.V in The Netherlands since June 1991.Visit his home page at http://mirrors.develooper.com/hpux
[Hack #21]
Jesse Vincent doesn't drink that often, but does find that a drink
or two tends to improve the CPAN remarkably [Hack #37]
Joe McMahon is a test automation architect at Yahoo! whosebackground includes extensive development experience: 25
years of work at NASA, with everything from spacecraft groundcommunications systems to Web development He has
contributed to core Perl test support, supplied the debuggersinternal documentation, and supports several CPAN modules,from App::SimpleScan and WWW::Mechanize::Pluggable to
Date::PeriodParser and GraphViz::Data::Structure [Hack #59]
Joshua ben Jore came to Perl because he had to write a CGIapp for a political campaign and he'd heard that Perl was bestfor that sort of thing He stayed when he found out for himself
Trang 12learning what Prolog has to offer He is well-known at
perlmonks.org under the handle diotalevi and admits to being aMorris dancer [Hack #83]
weekly) [Hack #18], [Hack #97]
Ricardo Signes trained for a career in philosophy, but the
sudden onset of the Industrial Revolution forced him to workwith baser forms of logic He lives in Bethlehem, Pennsylvaniaand writes Perl for Pobox.com [Hack #8]
Sean M Burke is a perfectly normal human being, based onorganic molecules, and exhibiting bilateral symmetry He is
typical He has written two O'Reilly books, Perl and LWP and
RTF Pocket Guide, has contributed dozens of modules to CPAN,
and was a regular columnist for The Perl Journal for about five years (His articles appear in the various O'Reilly Best of the
Perl Journal books, notably the Games, Diversions & Perl
Culture volume.) Trained as a linguist, he now lives on an island
in southeast Alaska, where he develops tools for Native
Trang 13http://interglacial.com [Hack #3], [Hack #6], [Hack #11]
Simon Wistow currently herds London.pmthe largest and mostrowdy of all the Perl Monger groups In his spare time he writesrather too many CPAN modules of dubious usefulness [Hack
#39]
Stephen B Jenkins (a.k.a Erudil) is the senior
programmer/analyst at the Aerodynamics Laboratory of theInstitute for Aerospace Research, National Research Council of
Canada He has written for Dr Dobb's Journal, Linux Journal,
The Perl Journal, the USENIX Association, the American
Institute of Aeronautics and Astronautics, the Aerospace andElectronics Systems Society of the IEEE, and the Association forComputing Machinery For more info, see
http://www.Erudil.com [Hack #101]
Tim Allwine is a Senior Software Engineer at O'Reilly Media, Inc
He develops software for the Market Research group, writesvarious spidering tools that collect data from disparate sites,and is involved in the development of web services at O'Reilly.[Hack #71]
functional programming, at http://blog.moertel.com [Hack
#61]
Brett Warden, BSEE, specializes in Perl programming and
embedded/telco systems He lives in the Northwest U.S with
Trang 14go to my friends, family, and commiserators, especially the U of
P crew and Aleatha, Esther, Eva, Kate, Katie, Laura, and Mel.Thanks and respect go to my work and hobby colleagues,
including all of the programmers who have invented and refinedthese tools which allow us to work and play
Finally, I dedicate this book to my family, Brett, Danielle, Jacob,Jack, and Brad in the hope that I am a worthy brother, brother-in-law, uncle, and cat caretaker
Damian Conway
Trang 15build this wonderful new Perl jungle gym To Larry Wall, whoinvented the extraordinary playground upon which it is
constructed And to my many, many friends throughout the
worldwide Perl community, whose encouragement and supporthas allowed me to play here for the past decade
Above all, to my beloved Linda, whose love, beauty, and graceilluminate every day of my life
Curtis "Ovid" Poe
There's no way I could have participated in this without the Perlcommunity having patiently answered so many of my silly
questions when I started learning Perl years ago They didn'tjust teach me Perl, they taught me how to be a programmer.Many thanks to chromatic and Allison for shrink-wrapping mymonkey I'll never forgive you Mostly, though, I want to thankSean and Lil, two people without whom the world would be asadder place for me
Technical Reviewers
The authors extend tremendous thanks to the technical
reviewers who found many errors, suggested many rephrasings,and argued over important formatting details to make this bookclear, accurate, and interesting They are Ann Barcomb, DanielBosold, Brad Bowman, Philippe "BooK" Bruhat, the #cgiapp IRCchannel, Chris Dolan, Michael Graham, Garick Hamlin, Joshuaben Jore, Yves Orton, Ricardo Signes, and David Wheeler
Trang 16Perl is a language with a rich and expressive vocabulary Sinceits original release in 1987, it's moved from quick-and-dirty
extraction and reporting to web programming, data munging,GUI building, automation gluing, and full-blown application
Army chainsaw
development It's the duct tape of the Internet and a Swiss-Like duct tape and multitools, Perl can do just about anythingyou can imagine and really want to do
If you just want to get your job done quickly, you can write thesimplest, easiest Perl you know and go on to other things Ifyou want to build big applications, you can do thatwith someexperience and a little discipline If you want to solve your
problem and don't mind a little help, the CPAN is there to giveyou a hand
That's all very productive, and being productive can be
fulfilling but Perl can also be fun
Imagine a litter of kittens, tumbling across the floor in a ball ofteeth and claws and fur and tiny little growls They're playing,sure, but they're also practicing the skills they need to survive
in the scary wild world They're careful not to hurt each other,but the tactics and surprises of one clever kitten can teach theothers valuable lessons
What makes a Perl guru? It's knowledge, partly, but it's mostlythe curiosity to play with the language, discover surprises, andeven invent a few of your own That's why this book was somuch fun to write Here are 101 tips, tricks, and techniquesfrom some of the best Perl programmers in the world Some areimmediately productive Some are sneak attacks that you mightonly use when you have no other choice Most of them have
Trang 17It's good to be productive That's why you program in Perl Add
in the fun of learningespecially lessons it took these Perl gurus
years to learnand you'll be ready for anything Amaze your
friends Astound your coworkers Walk into the jungle of codeand specifications and customer requests with the confidencethat you can take down any problem that jumps out at you.You will
done O'Reilly's Hacks series is an attempt to reclaim the word,document the ways people are hacking (in a good way), andpass the hacker ethic of creative participation on to a new
generation of hackers Seeing how others approach systemsand problems is often the quickest way to learn about a newtechnology
It's also fun
Of course, no single book could possibly document all of theinteresting and creative and mind-expanding things people canand do achieve with Perl but we hope this book will put you inthe right mindset to hack your own crazy ideas
Trang 18We've divided this book along various topics, not according toany sense of relative difficulty Skip around and flip through thebook; if you see an interesting title or some paragraph catchesyour eye, read it! Where possible, we've added cross references
to related hacks in the text For example, if you and your
coworkers are right now celebrating beer-thirty on a lazy Fridayafternoon, start with "Drink to the CPAN" [Hack #37]
How This Book Is Organized
Chapter 1, Productivity Hacks
Admit it When you sit down to code, something annoysyou It may be repetitive commands, looking up
documentation, or the fact that your coworkers just can'talign their equals signs Getting rid of those interruptionsand annoyances will make your life easier and your codingsessions more enjoyable This chapter suggests a few ideas
Chapter 2, User Interaction
Menus, graphics, beeps, and command lines: these are allways your programs grab user attention Do it kindly Do itwith style Do it with convenience This chapter is all aboutworking with other people
Chapter 3, Data Munging
What does Perl love to extract and report? Data! Hook it up
Trang 19polish your Perl to slice and dice data again
Chapter 4, Working with Modules
Perl 5's unit of reusable code is the module When you
unleash the power of moduleswhether your own or thosewritten by other peopleyou're on track to becoming a realguru To do that, you need to go beyond the idea "one file,one namespace, one module." You need to understand whatmakes them work and how to exploit all of the clever
possibilities they provide This chapter leads the way
Chapter 5, Object Hacks
Abstraction, encapsulation, and genericity are the keys todesigning large, maintainable systems Objects are one way
to go Sure, some people claim that Perl doesn't really do
OO, but they're wrong and you can prove it Go beyond theblessed hashes and find ways to build powerful abstractions
in this chapter
Chapter 6, Debugging
Of course your code works, but someday you'll have to digthrough a pile of Perl left by an obnoxious coworker (even ifthat was you before you read this book) Don't go in
unarmed Prepare yourself for the worst with a toolkit full oftips and techniques to disarm the hairiest, weirdest codeyou can imagine This chapter will shine light on those
lurking monsters (and they're not so bad once you get to
Trang 20Chapter 7, Developer Tricks
Maintaining a program is different from maintaining an
entire system This is doubly true if you work with otherpeople If anything, discipline and consistency are moreimportant than ever Of course, so are clever ways to
really want to take advantage of the deeper mysteries of
Perl, you have to be able to look deeply into the language,the libraries, and the interpreter itselfas well as your owncodeand understand what's happening This chapter willtake you from programmer to guru Hang on
Chapter 9, Expand Your Perl Foo
When the phrase "Perl Fu" just isn't metasyntactic enough,it's time to move on to "Perl Foo." Almost no one has
explored every corner of Perl There's just too much to
learn Until now there have been no maps This chapter willfill your head with a few of the odder ideas in the world ofPerl Then you'll be ready to discover your own
Conventions Used in This Book
Trang 21Indicates commands, options, switches, variables,
attributes, keys, functions, the contents of files, and theoutput from commands
Trang 22This icon signifies a tip, suggestion, or general note
This icon indicates a warning or caution.
The thermometer icons, found next to each hack, indicate therelative complexity of the hack:
Using Code Examples
This book is here to help you get your job done In general, youmay use the code in this book in your programs and
documentation You do not need to contact us for permissionunless you're reproducing a significant portion of the code Forexample, writing a program that uses several chunks of codefrom this book does not require permission Selling or
distributing a CD-ROM of examples from O'Reilly books doesrequire permission Answering a question by citing this bookand quoting example code does not require permission
Incorporating a significant amount of example code from thisbook into your product's documentation does require
permission
We appreciate, but do not require, attribution An attribution
Trang 23example: "Perl Hacks by chromatic with Damian Conway and
52674-1."
Curtis 'Ovid' Poe Copyright 2006 O'Reilly Media, Inc., 0-596-If you feel your use of code examples falls outside fair use orthe permission given above, feel free to contact us at
We'd Like to Hear from You
Please address comments and questions concerning this book tothe publisher:
Trang 24To comment or ask technical questions about this book, sendemail to:
bookquestions@oreilly.com
For more information about our books, conferences, ResourceCenters, and the O'Reilly Network, see our web site at:
http://www.oreilly.com/
Trang 25
Hacks 1-11
Everyone wants to be more productive That's probably why youuse Perl: to get more work done in less time with less work
Productivity isn't all about saving time, though Saving effort iseven more important, whether you mean finding the
Here are a few ideas for ways to make your programming lifeeasier and more productive Try them, enjoy your new sense offree time, and let yourself notice the new points of friction inyour life Then solve them, too!
Trang 26Open the bookmark menu in your browser; this is BookmarksManage Bookmarks in Mozilla Firefox Create a new bookmark
Trang 27Location box, type:
http://search.cpan.org/search?mode=module;query=%s
Figure 1-1 shows the completed dialog box Press OK, then goback to the browser Clear the location bar, then type cpanAcme
and hit Enter This will take you immediately to the first page ofsearch results for modules with Acme in their names
Figure 1-1 Creating a new keyword bookmark
search
Read Module Documentation
If you know exactly the name of the module you want, it's moreconvenient to jump straight to information about that module
Trang 28Create a new bookmark named Show Module Documentation, with the
keyword of cpod and the location:
http://search.cpan.org/perldoc/%s
Press OK, then type cpod Test::Builder and press Enter You'll see
the latest version of the Test::Builder documentation
Trang 29Hacking the Hack
The keyword search feature of Firefox turns your browser'saddress bar into a command line It's simple to write your ownCGI script or mod_perl handler to add a new command to thebrowserall it has to do is take a query string and return
information You could easily write code to implement a singlecommand that aggregates different documentation sources (forexample, you can search JavaScript and HTML and Perl
documentation with a single query)
The URL of the bookmark can be a javascript: URL that runscode in the browser In essence you're creating a bookmarkletthat you trigger on the command line You could use JavaScript
to open the search results in a new window or tab or search forthe currently selected text
Trang 30
In a decent pager, such as less on a Unix-like system, use the forward slash ( / ) to begin a search Type the rest of the name and hit Enter to begin searching Press n to find the next occurrence and N to find the previous one.
Why search yourself, though? perldoc's -f switch searches
perlfunc for you, presenting only the documentation for the
named operator Type instead:
Trang 31
The program will launch your favorite pager, showing only thedocumentation for substr Handy
Answer a FAQ
The Perl FAQ is a very useful piece of the core documentation,with a table of contents in perlfaq and nine other documents(perlfaq1 through perlfaq9) full of frequently asked questions andtheir answers
Searching every document for your question, however, is moretedious than searching perlfunc (Do skim perlfaq once in a while
$ perldoc -q "Perl ?6"
Trang 32
The quotes prevent the shell from interpreting the space as an argument separator.
Webify It
Maybe the command line isn't your thing Maybe you work in agroup of programmers who won't leave their comfortable IDEslong enough to type a few commandsand who certainly won'tread documentation from anywhere but the IDE or a web page
That's okay perldoc can produce HTML (or any other type ofoutput for which you have a POD translator installed), too Usethe -o switch with your preferred output format To turn perltoc
Trang 33document) and print its location instead of formatting anddisplaying the text Here's where Test::Tutorial and perlunintro
Trang 34Browse the Code
perldoc -l is pretty useful, especially if you want to know where
a module is, so that you can look inside it One more switchmakes that even more useful, however The -m option shows theplain, unrendered text of the named module or document inyour favorite pager
If you suspect that the author of Test::MockObject has hidden
Likewise, the -u option shows only the unformatted POD source, without the code.
Trang 35
Host your own HTML documentation.
perldoc is a fine way to view the documentation for Perl and allyour installed modules and to output them in the file format ofyour choice ("Put Perldoc to Work" [Hack #2]) perldoc's littlebrother, podwebserver, is an even handier way to browse
documentationand bookmark it, and search it, and sometimeseven hardcopy it, all through whatever web browser you're
browser in Emacs In fact, there have been persistent rumors ofsome users adventurously accessing podwebserver via cell phones,
Trang 36To run podwebserver, just start it from the command line You
don't need root access:
$ podwebserver
Then start a web browser and browse to http://localhost:8020/.You'll see the index of the installed documentation (Figure 1-2)
Figure 1-2 An index of your Perl documentation
If you don't want to bind the web server to localhost, or if youhave something already running on port 8020, use the -H and -p
arguments to change the host and port
Trang 38
Make programming easier by programming your shell.
Perl is a language for people who type It grew up from theshell to write all kinds of programs, but it still rewards peoplewho don't mind launching programs from the command line
If you spend your time writing Perl from the command line
(whether you write short scripts or full-blown programs),
spending a few minutes automating common tasks can saveyou lots of development timeand even more trouble
Trang 39If you prefer tcsh, edit your cshrc file Then replace the = sign with a single space in all of the alias declarations.
Launch a new shell Type the command realias and your favorite
editor (assuming you have the EDITOR environment variable set,
Trang 40Useful Shell Aliases
What makes a good shell alias for Perl programming? Obviously
a command that's difficult to remember, such as the one to putthe Perl debugger into pseudo-interactive mode Another goodapproach is to alias commands that are lengthy or otherwisedifficult to type One final category is a series of chained
commands you find yourself typing often
Here are a few examples Change the paths as necessary, ofcourse, but have fun removing a little more of the tedium fromyour life every time you notice yourself repeating somethingyou could automate away That's the Perl way
Juggle multiple Perl versions
Suppose you're in the midst of upgrading Perl versions whileyou still have to maintain an older installation You might havemultiple versions of Perl installed Instead of typing differentpaths all the time and instead of relying on tab completion todifferentiate between perl5.8.8 and perl5.6.2 and so on, make thenames different at the start:
alias newperl='/usr/local/bin/perl5.8.8'
alias oldperl='/usr/local/bin/perl5.6.2'
This is especially handy if you have a system Perl installed anddon't want to break things by overwriting it
Juggle multiple module versions