November 20th, 2008


stupid perl

I just spent at least an hour debugging a problem that I can blame squarely on Perl. Consider the following declaration:

  %lookup = {
    A => 1,
    B => 2

This looks like a perfectly reasonable hash initialisation, right? However, trying to actually reference anything that should be in this hash results in undef. What's the problem?

The answer is I used the wrong kind of brackets! Even though curly brackets are used for all other hash type things, you can't use them here because they mean something completely different and unexpected. The correct declaration is:

  %lookup = (
    A => 1,
    B => 2

It turns out that on the platform where I'm doing this, I have to use a really really old version of Perl (5.002). The -w switch apparently doesn't detect this problem in 5.002. In later versions, you will at least get this sort of warning:

  $ perl -w -e '%a = {}'
  Reference found where even-sized list expected at -e line 1.

Bloody hell. I hate Perl. Years ago when I was writing Perl stuff regularly I would have remembered this, but it's just not the sort of thing you recall after using sensible languages in the meantime.