I'm in the midst of writing code to search wordlists. I re-invented the Search::Dict wheel, but since there were no significant improvements I abandoned that since it's core anyway. What I did do come up with is a a class for Search::Dict so that I could tie a hash to it. To do lookups into the dictionary simply:
use WordList; tie %h, "WordList", "/usr/dict/words"; if (exists $h{foo}) { print "It's there!" } if ($h{foo}) { print "There too!" } print keys %h;
And so on. If the key is in the file, it returns true. Otherwise false. Want case insenitivity? Have at it. My lists were all lowercase. :)
package WordList; use Search::Dict; use strict; sub TIEHASH { my($class, $dictionary)=@_; open(D, $dictionary) || die "Can't open $dictionary: $!"; my $self={ file => do { *D }, 'each' => undef }; bless $self, $class; } sub EXISTS { my($self, $key)=@_; my $o=look $self->{file}, $key, 0, 0; local chomp($_=readline $self->{file}); if ($_ eq $key) { return 1; } return; } sub FETCH { $_[0]->EXISTS($_[1]); } sub FIRSTKEY { $_[0]->{'each'}=0; $_[0]->NEXTKEY; } sub NEXTKEY { my($self)=@_; seek($self->{file}, $self->{each}, 0) || warn; local $_=readline $self->{file}; if (! defined $_) { return $self->{each}=undef; } $self->{each}=tell($self->{file}); chomp; return $_; } sub STORE {} sub DELETE {} sub CLEAR {} 1;