diff options
author | jluehrs2 <jluehrs2@uiuc.edu> | 2008-02-01 01:20:19 -0500 |
---|---|---|
committer | jluehrs2 <jluehrs2@uiuc.edu> | 2008-02-01 01:20:19 -0500 |
commit | 917b8b98497bf3f685883b01494475c53c2d944b (patch) | |
tree | 74f149bc522b4d4418aa931ae94272d98dc916b6 | |
parent | a58fc188f9cd24bdb290c6d9d9e325a09554e572 (diff) | |
download | games-word-917b8b98497bf3f685883b01494475c53c2d944b.tar.gz games-word-917b8b98497bf3f685883b01494475c53c2d944b.zip |
allow the constructor to fill up the word list from an arrayref
-rw-r--r-- | lib/Games/Word/Wordlist.pm | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/lib/Games/Word/Wordlist.pm b/lib/Games/Word/Wordlist.pm index 0ae24a9..51ee9b6 100644 --- a/lib/Games/Word/Wordlist.pm +++ b/lib/Games/Word/Wordlist.pm @@ -8,9 +8,6 @@ use List::MoreUtils qw/uniq/; sub new { my $class = shift; my $word_list = shift; - die "Can't read word list: $word_list" unless -r $word_list; - die "Empty word list: $word_list" unless -s $word_list; - my $self = { cache => 1, @_, @@ -19,13 +16,24 @@ sub new { word_list => [], word_hash => {}, }; - if ($self->{cache}) { - open my $fh, $word_list or die "Opening $word_list failed"; - for (<$fh>) { - chomp; - $self->{word_hash}{$_} = 1; + + if (ref($word_list) eq 'ARRAY') { + $self->{cache} = 1; + $self->{file} = ''; + $self->{word_list} = $word_list; + $self->{word_hash}{$_} = 1 for @$word_list; + } + else { + die "Can't read word list: $word_list" unless -r $word_list; + die "Empty word list: $word_list" unless -s $word_list; + if ($self->{cache}) { + open my $fh, $word_list or die "Opening $word_list failed"; + for (<$fh>) { + chomp; + $self->{word_hash}{$_} = 1; + } + $self->{word_list} = [keys %{ $self->{word_hash} }]; } - $self->{word_list} = [keys %{ $self->{word_hash} }]; } bless $self, $class; |