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 ]

acme (189)

acme
  (email not shown publicly)
http://www.astray.com/

Leon Brocard (aka acme) is an orange-loving Perl eurohacker with many varied contributions to the Perl community, including the GraphViz module on the CPAN. YAPC::Europe was all his fault. He is still looking for a Perl Monger group he can start which begins with the letter 'D'.

Journal of acme (189)

Thursday June 14, 2001
10:40 AM

Tie::GHash

[ #293 ]
First, check out the docs to Tie::GHash.

The story behind this module is interesting. This module was created during Brian Ingerson's Inline talk at YAPC::NorthAmerica 2001 when he asked me to code something fun using Inline, so blame him for this.

To be fair, I'd been thinking about the idea for this module for a while. Perl hashes take up a lot of memory, and occasionally you'd like to have huge hashes in memory (for Markov chains, say).

I had been aware that there were various different hash libraries around, so I picked a random one which was likely to be installed on a computer: the GNOME glib's ghash library. I checked out the API docs and it seemed fairly easy to use. I found an article explaining the rest of the details, and everything looked sweet.

Now I needed to code up a basic Inline module (using the new Inline::MakeMaker module). I did a simple h2xs incantation, changed the Makefile.PL to use Inline::MakeMaker and WriteInlineMakefile, set up a module as documented in Inline.pod, and then read the Inline::C cookbook to learn "How can I use Inline to make use of some shared library?".

While I could have provided an object-oriented version to these hashes, it was far better to make the hashes look just like Perl internal hashes. This can be acheived by tie-ing the hash - see the perltie documentation. Basically, all you need to do is set up various functions such as TIEHASH, STORE, FETCH, etc. What's cute is that with Inline I simply code these functions in C (there is a little wrapping of the glib hash functions, but only a little).

Being a good little extreme programmer, I had already written a test suite. With 511 tests. I built the module. It all worked. I worried.

This was now halfway through Brian's talk and there was a break, so I showed him the code. He ooooh-ed and aaaaah-ed. I spent the rest of his talk cleaning it up, writing documentation, improving the tests, writing some benchmarks, writing to modules@perl.org about the name, building the distribution, etc. I managed to upload it to CPAN just as Brian was finishing his talk.

So there you go. One of the first modules on CPAN that uses Inline. So who's next? ;-)