Author image Jesse Luehrs
and 1 contributors


Games::Word::Wordlist - manages a list of words


version 0.06


    use Games::Word::Wordlist;
    my $wl = Games::Word::Wordlist->new('/usr/share/dict/words');
    my $word = $wl->random_word;
    print "we have a word" if $wl->is_word($word);


Games::Word::Wordlist manages a list of words, either from a wordlist file on your computer or from a list of words you specify. You can use it to query things about the list, such as whether a given string is a word, or how many words are in the list, and you can also get words from the list, either randomly or by regex.



The constructor initializes the word list with words, either from a file or from an arrayref, given as the first argument. The remaining arguments are a paramhash which recognizes these options:


Whether or not we want to keep an in memory cache of the words in the word list. Defaults to true, since this is what we want in almost all cases, as it speeds up lookups by several orders of magnitude. Can be set to false if either memory is a significant issue (word lists can be several megabytes in size) or if the word list is expected to change frequently during the running of the program.


Add words to the word list. Only works if the word list has been cached.

Takes either a reference to an array containing the new words, or the name of a file to read in the words from. Ignores any words already in the word list.

remove_words LIST

Removes words in LIST from the word list. Only works if the word list is cached.


Returns the number of words in the word list.

random_word [LENGTH]

Returns a random word from the word list, optionally of length LENGTH.

is_word STRING

Returns true if STRING is found in the word list, and false otherwise.

each_word CODE

Call CODE for each word in the word list. The current word will be passed into CODE as its only argument.

anagrams STRING

Returns a list of all permutations of STRING that are found in the word list.

words_like REGEX

Returns a list containing all words in the word list which match REGEX.

subwords_of STRING

Returns a list of words from the word list which can be made using the letters in STRING.




Jesse Luehrs <doy at tozt dot net>


This software is copyright (c) 2012 by Jesse Luehrs.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.