On my desk right now

cloudnine on 2005-06-16T17:47:43

I realize that I will be long dead before I understand all there is to know about perl and unix. But I figure I might as well do my best, and maybe help some people out along the way. Lots of guru's have lent me a hand, and I would like to do my part. So here is a bit of information that maybe a beginner / intermediate perl programmer could use.

I've generally found that my understanding happens in sudden leaps, i.e. those 'Eureka!' moments, with long plateaus in between, where I would spend time applying my new understanding and getting into all sorts of trouble. :)

About a year ago, I found myself approaching the end of a plateau. I was becoming interested in how unix processes actually worked, because at the time I was working on version 2 of a homegrown spam filtering system. I wanted to implement it as a daemon, so I could eliminate the overhead of firing up a perl process for each mail I had to scan. Long story short ... While I love the camel book, it does nothing to explain the 'how' and 'why' of the unix system calls and networking functions. Questions like 'how does fork or exec work', and 'what the heck is PF_INET and where does it come from' were two of the constant questions I needed answers for.

It didn't help that the Camel book says things like the following, in the description for the 'socket' function: "This function opens a socket of the specified kind and attches it to a filehandle SOCKET. DOMAIN, TYPE, and PROTOCOL are specified the same as for socket(2)."

Granted that explanations of Unix syscalls are far beyond the scope of the camel book - but I needed some answers. I knew nothing of the details of DOMAIN, TYPE, and PROTOCOL, and the socket(2) manpage isn't exactly forthcoming either. It became clear that I needed to understand what was going on behind the scenes before I could continue with my project. I needed to understand, to some degree, the Unix that was happening, which would involve understanding a bit of C as well.

SHORT ANSWER TO MY PROBLEMS: "Advanced Programming in the UNIX Environment" [APUE], by W. Richard Stevens. This book not only goes through unix from a C programmer's perspective, it explains the system calls in clear english. And hooray for Perl, because the Unix system calls that perl makes available to the user look pretty much like the C library calls. Which means that a Perl programmer should be able to grok the examples without too much of a stretch. Anything that Perl's standard function library lacks (e.g. fully unbuffered pipes) can be found in the POSIX module.

APUE talks about how Unix really works. It goes into unbuffered and buffered I/O, proccess accounting, interprocess communication, signals ... all the goodies that I wanted to understand are in this book. I'm not ready to truly grok at least half of this book yet - but it's a work in progress, and what I have learned so far has been invaluable.

If your C is rusty or nonexistant, pick up K&R C, and the Harbison and Steele C book first. You may also like the O'rielly Practical C Programming book as well, though I found that it wasn't all that practical. C is worth the journey, and I'm only a few steps down the road - learning C has made me a better Perl programmer, and has given me a more solid understanding of computers in general.

Even a small understanding of C will help you get through some of the Stevens book, though in general, APUE is extraordinarily good at explaining itself, with copious examples and crystal clear writing style.

I hope all this info helps. Happy hacking.

Just for fun, here are the currently invaluable tools on my desk, in no particular order:

Google Programming Perl Perl Cookbook Advanced Programming in The Unix Environment perldoc -f


In addition

bart on 2005-06-17T05:23:39

"Network Programming with Perl" by Lincoln Stein is the one book in Perl that I know of, that goes pretty deep into the same subjects: sockets, TCP, daemons, zombies...

In fact, the book is often compared to W. Richard Stevens' books. From the back of the cover:

Network Programming with Perl focuses on TCP/IP rather than just the common Web protocols. Modeled after the critically acclaimed TCP/IP Illustrated by W. Richard Stevens, this book achieves a level of detail far superior to most. It is an essential resource for network administrators and Perl programmers who are creating network applications.

If anything, it fills the gap between W. Richard Stevens' books, and the common Perl books, very nicely.