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

  • fault line flyers video online

    A couple of weeks ago, Fox 7 News went out to Fault Line Flyers (the soaring club where I used to fly in Texas) and filmed a bunch of footage for a…

  • pumpkin drop!

    Tomorrow is the annual Pumpkin Drop at the glider field! A pilot and a bombadier go up in a two-seat glider. The bombadier sits in the back with a…

  • private pilot checkride

    This year has been an especially disappointing year for soaring. Between uncooperative weather, my summer vacation to Spain, occasional lack of…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded