NAME
Tie::DictFile - tie a hash to local dictionary file
SYNOPSIS
use Tie::DictFile;
tie %hash, Tie::DictFile;
if(exists $hash{'aword'}) {
print "aword is in dictionary\n";
}
$hash{'newword'}=1;
delete $hash{'spell'};
untie %hash;
DESCRIPTION
Ties a hash to a local dictionary file (typically /usr/dict/words
or /usr/share/dict/words
) to allow easy dictionary lookup, insertion and deletion. Lookup operations are cached for performance, and any insertions/deletions are only written to the dictionary file when the hash is untied or DESTROY'ed.
By default, a hash is tied to the dictionary file specified by $Tie::DictFile::DICTIONARY
. Pass a third argument to tie
to specify an alternative file, eg:
tie %hash, Tie::DictFile, '/usr/dict/words';
Dictionary lookups can either be performed by using the exists
function, eg:
exists $hash{'appetite'} ? "yes" : "no"
or by directly attempting to fetch the hash element:
defined $hash{'appetite'} ? "yes" : "no"
New words can be added to the dictionary by assigning any non-undef
value a hash element, eg:
$hash{'KitKat'}=1;
Words can be deleted from the dictionary, either by assigning undef
to the hash element:
$hash{'KitKat'}=undef;
or
undef $hash{'KitKat'};
or by using the delete
method:
delete $hash{'KitKat'};
When the hash is untied (or DESTROY'ed as it goes out of scope), the module will attempt to write the requested insertions and deletions to the dictionary file. The module will croak
if the correct write permissions have not been set.
Case sensitivity
Searches are performed in a case-insensitive manner, so $hash{'foo'}
and $hash{'Foo'}
return the same result. The result will either be matching word in the dictionary file:
$hash{'CraZy'} eq 'crazy'
or the key which was used to assign a new hash element which is not already present in the dictionary file, eg:
$hash{'KitKat'}=1;
$hash{'kitkat'] eq 'KitKat'
Options
To enhance performance, it is assumed that the dictionary has a maximum word length of 62 characters (which biases lookups towards more seek
's against readline
loops). This assumption can be changed by assigning the variable:
$Tie::DictFile::MAX_WORD_LENGTH
Another performance enhancement is to cache any words encountered in the dictionary file. Only the 1024 most recent words are cached. To enhance performance (at a cost of memory), re-assign the variable:
$Tie::DictFile::CACHE_SIZE
SEE ALSO
AUTHOR
Alex Nunes <cpan@noptr.com>
CREDITS
Elements of the lookup code are based on Jarko Hietaniemi's Search::Dict module.
BUGS
Does not address concurrent writes to the dictionary file.
Will not behave properly with a file whose lines are not sorted in dictionary order.