Perl is such a great language, except for when it’s not. There’s this problem, which is best illustrated by example: a colleague came into my office with a testy expression and said “You wrote this; what the $#@!%! does it do?!?” I told him I’d get right back to him. [Update: I get spanked.]

@fields = (0, @fields);
while ($r = $q->fetchrow_arrayref)
  my $topic = $r->[0];
  while ($topic != -1)
    my $i;
    foreach $i (1 .. $#{$r})
      $totals{$fields[$i]}->[$topic] += $r->[$i];
    $topic = $parents[$topic];
shift @fields;

Er, well, it aggregates a bunch of fields in rows of a relational database where the rows have integer keys and a parent-child relationship. And does it a couple of orders of magnitude more efficiently than the previous try, which crammed the work down into the SQL engine.

This is the Perl conundrum. It gets the job done. It gets the job done in a hurry, and I don’t know about you, but I’m usually in a hurry. But, you might hate yourself in the morning.

Now I’ll get email from dozens of Perl gods explaining that (a) this is perfectly clear and (b) I could have done it better in three lines of code. Then I’ll get twice as much from Python gods showing how much clearer it would have been in Python, and in only two lines of code. Sigh.

Update: Comment, Stupid! · A few people, including Nathan Torkington and Jeremy Zawodny, chided me for not having bothered to comment the code. And indeed they’re right, and I’ve personally given grief to more than one junior programmer along the same lines.

But this is part of the trouble I routinely get myself into with Perl. There’s this database we need to aggregate, right now, and the obvious straightforward SQL logic is going O(N2) (worked fine on less than a million records), who ya gonna call? You call the nearest Perl-slinger to whip something up ad-hoc (for example the above) but then it silently, sneakily, surreptitiously slips over the wall into production and is getting run thrice per day by some cron job, and everything’s fine till they add another field to the database... you’ve probably been there.

Which isn’t to say that I shouldn’t have commented that code. Which also isn’t to say that if I had, when they slapped down in front of me, I might not have looked just as blank; commenting this kind of opacity at the right level to make things clear is an art in itself.

author · Dad
colophon · rights
picture of the day
July 31, 2003
· Technology (90 fragments)
· · Coding (98 more)

By .

The opinions expressed here
are my own, and no other party
necessarily agrees with them.

A full disclosure of my
professional interests is
on the author page.

I’m on Mastodon!