Minimal Perl for Unix and Linux People | WebReference

Minimal Perl for Unix and Linux People


Perl as a (Better) Find Command

By: Tim Maher

Scene: Church basement, Seattle, USA. Raining—as usual.

A burly, unshaven, heavily tattooed man is tugging at the sleeve of a woman, who is standing at a podium. She responds to the sleeve-tugger with annoyance.

"Yes Lefty, I know you're upset that they're not serving the tea biscuits on those lovely lace doilies anymore, but given our cash-flow situation—sorry, we'll have to discuss this later.

Testing, testing, 1 2 3. Is this thing on?


Would you take your seats please, the meeting is about to begin.

Good evening! As you regulars know, we always begin by welcoming the newcomers. Do we have any first-timers here tonight?

Yes, you sir, with the bushy red hair, would you please introduce yourself to the group?"

[Camera zooms in on a bearded, bespectacled, amber teddy-bear of a man, obviously of Irish descent.]

"Hello, my name's Tim.

And I am a loser."

[Fade to black]

As much as I hate to admit it, that statement is 100 percent true! I really am a loser. What's worse, I am a chronic loser!

I don't mean that I'm a pitiful ne'erdowell who can never get his life in order. I mean that I lose things—all the time! Luckily for me, my wife has what psychologists call eidetic imagery, which is more commonly known as a photographic memory. All I have to do is ask her, "Have you seen my iPod lately?" and she'll consult her database of mental images and tell me exactly where it is.

Even if you're not a chronic loser like me, you've probably misplaced a file or two on a Unix system by now. This may have motivated you to learn about the find command, because it's used to locate and identify files that have certain specified attributes. In a sense, find is the Unix system's answer to having a partner with a photographic memory.

The find command can certainly come in handy. As a case in point, the other day I made some modifications to the standard Perl script that's used to convert documents from Perl's Plain Old Documentation format ( POD) to HTML. The new script worked nicely, and it instantly became a valuable addition to my toolkit.

But then I lost it! I couldn't remember it s name, or what directory I had stored it in. But I knew what its attributes were: owned by tim, file-type regular, name containing html, permissions of read, write, and execute for the owner, and modified in the last 24 hours.

So I issued the following find command, and it rapidly found the file for me:

In addition to being invaluable to Unix users, find is even more important to Unix system administrators, who would have a hard time managing their systems without it.

On the other hand, find has some annoying limitations, which have been known to motivate programmers to seek alternatives. What's more, you can only count on find being available on Unix systems, so once you grow dependent on it, you'll miss it when using other OSs.

Fortunately, you can easily write Perl programs that surpass find's limitations and extend its reach to non-Unix platforms. You'll see examples of many programs of this type shortly, but first we'll discuss why most of them take a different form than the programming examples shown thus far.