Friday, December 27 2002

Baby’s First Perl Module

My blushes. I’ve been hacking in Perl since version 2.0 was released. In some ways, it’s my native programming language. It’s certainly the one I use the most, and the tool I reach for first when I need to solve a problem.

But I haven’t kept up. Until quite recently, I was really still writing Perl3 with some Perl4 features, and regarded many of the Perl5-isms with horror. It felt like the Uh-Oh programmers had crufted up my favorite toy, and it didn’t help that the largest example of the “New Perl” that I had to deal with was massive, sluggish, and an unbelievable memory hog (over 9,000 lines of Perl plus an 8,000 line C++ library, and it uses up 3 Gigabytes of physical memory and 3 dedicated CPUs for its 25-hour runtime (“Hi, Andrew!”)).

I am a fan of reusing (and outright stealing) code, and the many modules at CPAN are very handy things. I just hadn’t really felt the need to write one myself. I’m usually writing standalone scripts, but I’ve learned the basics of Perl’s modules and methods by fixing other people’s scripts (including, alas, the monster mentioned above).

So what finally pushed me over the line? This site. The primitive prototype weblog you’re reading now [ed: since replaced with Movable Type] is hacked together from a few standalone scripts, a Makefile, and the XML::Simple module, with the articles stored as XML files.

This broke down quickly, because I don’t use a GUI front-end to type in my blog entries; I’m typing the XML by hand in Emacs, and manually escaping HTML entitities. I’m now quite sick of it.

The latest prototype is my online recipe database (built with Mason and SWISH-E), a few entries from which are linked in the navbar. I started by exporting my recipes from MasterCook as XML, parsing them, and converting to HTML. The problem was, I still had to use MasterCook to type in new recipes, and that meant firing up the Windows machine.

What I needed was a simpler way to store a set of key/value pairs in a text file, with arrays and multi-line values. It had to be fairly robust, so it could survive email and other common abuses, and it had to be able to encode all of the data present in both MasterCook recipes and RSS 2.0 feeds. And it had to be easy enough to type that I could convince my friends to use it for recipes.

What I ended up with was a module I call Data::TextFields. It’s not done, but it’s already pretty useful. I’ve already started converting the recipes to use it (although I’ll have to figure out how to coax SWISH-E into recognizing the fields when I build the index), and I’ll be converting this blog over Real Soon Now. [ed: the recipes are fully converted, the weblog took the road more traveled]

The next module will be a cleanup of the namespace-abstraction code I’m using for the recipe library, tentatively called HTML::Mason::Space. This brings together some of the stuff I’ve discussed before, about hiding implementation details in URLs and completely separating the URL namespace from the layout of the file system.