Perl for Puzzlers

I was working on creating a puzzle a couple of weeks ago and needed sets of a dozen 5-letter words that all “interacted” in a certain way (sorry, don’t want to give away too much).  One of the software guys here at work suggested a Perl script to find sets words, specifically Strawberry Perl. I have been programming since I was a kid (basic on a VIC-20) but lately have been resistant to learning new languages because I can usually accomplish my goals with the languages I already know. This time I decided to take his advice and was pleasantly surprised with the simple learning curve and thought I’d pass on a bit of intro on how to set up and start using Perl to help create puzzles.


I am not a Computer Scientist, nor do I play one on TV. This is a description of how I went about solving a specific problem, not necessarily the best method. For informational purposes only. Use at your own risk.

Installation of Strawberry Perl was completely straightforward on my XP machine. I haven’t tried any Win7 installs, but the home page suggests it is just as simple. I just downloaded the Windows installer and let it run. This will install Perl on your machine let you run scripts from the command line.

1. Create a folder to place all of my code and research material.

2.  Create a text file in the folder where I had all of my puzzle related info called “prompt.bat” (no quotes), and consisted of the single word “cmd” (again no quotes). Double-clicking this file will open a command prompt within the folder so you can launch the scripts easily.

3. Download a word list that can be easily read into your script. I used output from Tea, but I also found some other word lists here:

4. Create a text file (I prefer Notepad++ for text editing. Be sure to set the Language to Perl to get the correct syntax hi lighting) with the name of the puzzle and a “.pl” suffix. This file will be your Perl Script.

5. Start the script with “#!/usr/bin/perl” This tells the computer that it is a Perl script.

6. Here’s a script I wrote to find all the rotations of a word (anyone who saw my sample puzzle on the forum may recognize this) (Save target as… or you may get an error. No idea why). You can run this by typing “perl words.txt” at the command prompt.

#use strict;
my %words;
my $i;
open(DATAIN,”<$ARGV[0]”) || die “cannot open \”$ARGV[0]\”: $!”;
while(my $line = <DATAIN>){
$line =~ s/\s+//;
$words{$line} = 1;
foreach my $word1 (sort keys %words){
my $num=0;
my $word=$word1;
print “-$word\n”;
for($i = 1; $i < length($word1); $i++){
$word= rotate($word);
print “$word\n”;
sub rotate{
my $foo=shift;
my $out=substr($foo,1,length($foo)-1).substr($foo,0,1);
return $out;

The beginning of the script reads in the command line argument (words.txt in the example above) then reads the word list into memory. Specifically into a hash structure named words hash, but I’ll discuss that later. While reading the file into memory, it also strips carriage returns and unprintable characters.

Next the script steps through each entry in the hash (%words). It takes this entry, and rotates it using the rotate function, once for each letter in the word. This way we end up with all possible rotations.

In a future post I’ll cover some of the neat things you can do once you have a word list in memory.

Comments are closed.