Tie::DictFile - tie a hash to local dictionary file
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;
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.
/usr/dict/words
/usr/share/dict/words
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::DictFile::DICTIONARY
tie
tie %hash, Tie::DictFile, '/usr/dict/words';
Dictionary lookups can either be performed by using the exists function, eg:
exists
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:
undef
$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
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.
croak
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{'foo'}
$hash{'Foo'}
$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'
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:
seek
readline
$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
Tie::Dict, Search::Dict
Alex Nunes <cpan@noptr.com>
Elements of the lookup code are based on Jarko Hietaniemi's Search::Dict module.
Does not address concurrent writes to the dictionary file.
Will not behave properly with a file whose lines are not sorted in dictionary order.
To install Tie::DictFile, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Tie::DictFile
CPAN shell
perl -MCPAN -e shell install Tie::DictFile
For more information on module installation, please visit the detailed CPAN module installation guide.