From 917b8b98497bf3f685883b01494475c53c2d944b Mon Sep 17 00:00:00 2001 From: jluehrs2 Date: Fri, 1 Feb 2008 01:20:19 -0500 Subject: allow the constructor to fill up the word list from an arrayref --- lib/Games/Word/Wordlist.pm | 26 +++++++++++++++++--------- 1 file 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; -- cgit v1.2.3-54-g00ecf