Perl Hacks: Tips & Tools for Programming, Debugging, and Surviving - page 6


Preface

Perl is a language with a rich and expressive vocabulary. Since its 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 development. It's the duct tape of the Internet and a Swiss-Army chainsaw.

Like duct tape and multitools, Perl can do just about anything you can imagine and really want to do.

If you just want to get your job done quickly, you can write the simplest, easiest Perl you know and go on to other things. If you want to build big applications, you can do thatwith some experience and a little discipline. If you want to solve your problem and don't mind a little help, the CPAN is there to give you 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 of teeth 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 the others valuable lessons.

What makes a Perl guru? It's knowledge, partly, but it's mostly the curiosity to play with the language, discover surprises, and even invent a few of your own. That's why this book was so much fun to write. Here are 101 tips, tricks, and techniques from some of the best Perl programmers in the world. Some are immediately productive. Some are sneak attacks that you might only use when you have no other choice. Most of them have two parts: the immediate problem you need to solve right now and a deeper, subtler technique that you can adapt to other situations. All of them are worth studying.

It'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 code and specifications and customer requests with the confidence that you can take down any problem that jumps out at you.

You will.

Why Perl Hacks?

The term hacking has an unfortunate reputation in the popular press, where it often refers to someone who breaks into systems or wreaks havoc with computers. Among enthusiasts, on the other hand, the term hack refers to a "quick-n-dirty" solution to a problem or a clever way to do something. The term hacker is very much a compliment, praising someone for being creative and having the technical chops to get things done. O'Reilly's Hacks series is an attempt to reclaim the word, document the ways people are hacking (in a good way), and pass the hacker ethic of creative participation on to a new generation of hackers. Seeing how others approach systems and problems is often the quickest way to learn about a new technology.

It's also fun.

Of course, no single book could possibly document all of the interesting and creative and mind-expanding things people can and do achieve with Perl...but we hope this book will put you in the right mindset to hack your own crazy ideas.

How To Use This Book

We've divided this book along various topics, not according to any sense of relative difficulty. Skip around and flip through the book; if you see an interesting title or some paragraph catches your 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 Friday afternoon, 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 annoys you. It may be repetitive commands, looking up documentation, or the fact that your coworkers just can't align their equals signs. Getting rid of those interruptions and annoyances will make your life easier and your coding sessions more enjoyable. This chapter suggests a few ideas.


Chapter 2, User Interaction

Menus, graphics, beeps, and command lines: these are all ways your programs grab user attention. Do it kindly. Do it with style. Do it with convenience. This chapter is all about working with other people.


Chapter 3, Data Munging

What does Perl love to extract and report? Data! Hook it up to databases and set it loose on files. It's so easy you might not realize that your code is, well, slow and kludgy. That's okay. Take the tips in this chapter to heart and you can polish 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 those written by other peopleyou're on track to becoming a real guru. To do that, you need to go beyond the idea "one file, one namespace, one module." You need to understand what makes 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 to designing 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 the blessed 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 dig through a pile of Perl left by an obnoxious coworker (even if that was you before you read this book). Don't go in unarmed. Prepare yourself for the worst with a toolkit full of tips and techniques to disarm the hairiest, weirdest code you can imagine. This chapter will shine light on those lurking monsters (and they're not so bad once you get to know them).


Chapter 7, Developer Tricks

Maintaining a program is different from maintaining an entire system. This is doubly true if you work with other people. If anything, discipline and consistency are more important than ever. Of course, so are clever ways to convince other people to do the right thing. Need some leverage to get your shop in shape? This chapter has answers.


Chapter 8, Know Thy Code

"Know thyself," the inscription said. This is the oracle of Perl, not Delphi, so the admonition is a little different. If you 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 own codeand understand what's happening. This chapter will take 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 will fill your head with a few of the odder ideas in the world of Perl. Then you'll be ready to discover your own.

Conventions Used in This Book

This book uses the following typographical conventions:


Plain text

Indicates menu titles, menu options, and menu buttons.


Italic

Indicates new terms, URLs, email addresses, filenames, file extensions, pathnames, and directories.


Constant width

Indicates commands, options, switches, variables, attributes, keys, functions, the contents of files, and the output from commands.


Constant width bold

Shows commands or other text that you should type literally.


Constant width italic

Shows text that you should replace with user-supplied values.


Gray type

Used to indicate a cross-reference within the text.


This icon signifies a tip, suggestion, or general note.

This icon indicates a warning or caution.


The thermometer icons, found next to each hack, indicate the relative complexity of the hack:

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you're reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O'Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product's documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Perl Hacks by chromatic with Damian Conway and Curtis 'Ovid' Poe. Copyright 2006 O'Reilly Media, Inc., 0-596-52674-1."

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com.

Safari Enabled

When you see a Safari® Enabled icon on the cover of your favorite technology book, that means the book is available online through the O'Reilly Network Safari Bookshelf.

Safari offers a solution that's better than e-books. It's a virtual library that lets you easily search thousands of top tech books, cut and paste code samples, download chapters, and find quick answers when you need the most accurate, current information. Try it for free at http://safari.oreilly.com.

We'd Like to Hear from You

Please address comments and questions concerning this book to the publisher:

O'Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the U.S. or Canada)
707-829-0515 (international/local)
707-829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/perlhks

To comment or ask technical questions about this book, send email to:

bookquestions@oreilly.com

For more information about our books, conferences, Resource Centers, and the O'Reilly Network, see our web site at:

http://www.oreilly.com/