[x]Blackmoor Vituperative

Sunday, 2009-03-08

Doublet solver for Perl

Filed under: Programming — bblackmoor @ 23:27

I have translated my doublet solver for Python to Perl. Having already done the difficult part — figuring out what the script needs to do — putting it into a specific language is, aside from a “gotcha” now and then, mostly just grunt work. But I was curious how it would compare with the Python version. I expected it to take an hour or so, but it took me most of the evening, partly because I got distracted by Celebrity Apprentice, and partly because I had to re-think how to handle arrays of arrays (more on that later).

So here is a link to the script, for anyone who might find it educational (rename it to doublet.pl in order to run it), and the official Scrabble word list (you will need to unzip it, of course).

For instructions on how to use the script, run doublet.pl -h

For the rules of the puzzle, what the output looks like, and so on, read my blog entry for the doublet solver for Python.

Some observations:

I have been writing Perl for years: well over a decade. At the time I discovered it, it could do things which were not feasible any other way. When you need to drive a screw and all you have is a hammer, you use a hammer and you are grateful to have it. I was grateful to have Perl.

Times have changed. There are a number of other widely supported languages which do what Perl does with more elegance and less perversity: PHP and Python, to name a couple you might have heard of. None of these languages are perfect. I suspect that no language will ever be perfect, because human beings are not perfect. Nonetheless, if given the choice, I would not choose to use Perl now that there are, in my opinion, better alternatives available.

As an example of things that Perl does poorly when compared to other languages, compare how PHP handles arrays to how Perl handles arrays. I mentioned in my doublet solver for Python that I was a bit disappointed in how Python handles arrays (what Python calls “lists” and “dictionaries”), but I think Python is a step above Perl in this area, while PHP is superior to them both.

Because of the way Perl handles (or rather, does not handle) multidimensional arrays, I had to put the code through some contortions to translate the doublet solver: instead of an array of arrays, I had to choose between using an array of references to other arrays (which is cumbersome), or an array of strings that pretend to be arrays (which is inelegant). I chose the latter. It works, yes, but I can’t say that I am pleased with it. I suppose I could have used “hashes” (what Perl calls associative arrays), but that would have been even uglier than the string solution, in my opinion.

I should point out that in both this script and the Python doublet solver, I used very few comments. Partly that is because the scripts are so short, and partly because I think what they do is self-evident. In general, I think comments are useful for a relatively small number of cases:

  1. Documenting the API for reusing a function or object.
  2. Explaining decisions which may be counterintuitive or inobvious.
  3. Pointing out kludges which, for whatever reason, seemed appropriate at the time.
  4. Notes for later improvements.
  5. Explaining algorithms which may not be obvious.
  6. Explaining function calls or API calls where the names used for variables, etc., are cryptic or misleading.
  7. Explaining the purpose of code so obfuscated that it looks like line noise.

That last item shows up in Perl a lot. But in this case, I think I made the code as transparent as it can be.