From 752338be7644da462041deea124252c006c68248 Mon Sep 17 00:00:00 2001 From: jluehrs2 Date: Fri, 1 Feb 2008 01:24:46 -0500 Subject: add methods to add and remove elements from the word list, and to get the number of words --- lib/Games/Word/Wordlist.pm | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/lib/Games/Word/Wordlist.pm b/lib/Games/Word/Wordlist.pm index 51ee9b6..d6045db 100644 --- a/lib/Games/Word/Wordlist.pm +++ b/lib/Games/Word/Wordlist.pm @@ -40,6 +40,40 @@ sub new { return $self; } +sub add_words { + my $self = shift; + my $word_list = shift; + + die "Can't add words to a non-cached word list" + unless $self->{cache}; + if (ref($word_list) eq 'ARRAY') { + for (@$word_list) { + $self->{word_hash}{$_} = 1; + } + } + else { + open my $fh, '<', $word_list or die "Opening $word_list failed"; + for (<$fh>) { + $self->{word_hash}{$_} = 1; + } + } + $self->{word_list} = [keys %{$self->{word_hash}}]; +} + +sub remove_words { + my $self = shift; + for (@_) { + delete $self->{word_hash}{$_}; + } + $self->{word_list} = [keys %{$self->{word_hash}}]; +} + +sub words { + my $self = shift; + return unless $self->{cache}; + return @{$self->{word_list}}; +} + sub _random_word_cache { my $self = shift; my @word_list = @{ $self->{word_list} }; -- cgit v1.2.3-54-g00ecf