Greg Hewgill (ghewgill) wrote,
Greg Hewgill

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.

Tags: perl

  • perl and git

    Git gained another feather in its cap today: Perl now uses Git for version control. Some guy here in New Zealand spent a year building custom tools…

  • 2013 in review

    2013 is the year when everything changed. The biggest event was the birth of our daughter Lily. She was born prematurely in Shanghai while we…

  • 2012 in review

    2012 has been fairly quiet. Maybe it just seems that way because I haven't actually written anything new in this blog since last year's annual…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded