I haven't had a chance to read through your code yet, but I would suggest perusing the chatter surrounding MJD's Perl Quiz of the Week (Expert #22) published last year--
http://perl.plover.com/qotw/. The perl-qotw-discuss archives are a bit hard to wade through, but at least read the summary and problem text posted on perl-qotw.
Good luck!