Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

cloudnine (5985)

cloudnine
  (email not shown publicly)

Journal of cloudnine (5985)

Thursday July 06, 2006
11:45 PM

treating a string like a file

Seems there's always something new to learn. Old, but interesting news I just found out. Pre perl 5.8, one would use IO::String to treat a string like a file. With perl 5.8, one can do this using native open:

my $string = "Foo\nBar\nBaz";
open(my $fh, '<', \$string) or die "DOH: $!";

my $line1 = <$fh>;
my $line2 = <$fh>;

## $/ is of course obeyed ....

Next thing you know, there will be a musical genre named grunge ....
Thursday June 16, 2005
12:47 PM

On my desk right now

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