summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjluehrs2 <jluehrs2@uiuc.edu>2008-01-31 18:15:22 -0500
committerjluehrs2 <jluehrs2@uiuc.edu>2008-01-31 18:15:22 -0500
commita67b45637a95c463661b777d2a4cc0608b5c298a (patch)
tree0baa92270ef11c83ccdd19de4327f1931fa894ff
parent7cb5d2c090896af488247b5e156e0067b66f8917 (diff)
downloadgames-word-a67b45637a95c463661b777d2a4cc0608b5c298a.tar.gz
games-word-a67b45637a95c463661b777d2a4cc0608b5c298a.zip
move the wordlist stuff into its own file
-rw-r--r--lib/Games/Word.pm73
-rw-r--r--lib/Games/Word/Wordlist.pm159
2 files changed, 160 insertions, 72 deletions
diff --git a/lib/Games/Word.pm b/lib/Games/Word.pm
index 19593da..d9f4801 100644
--- a/lib/Games/Word.pm
+++ b/lib/Games/Word.pm
@@ -2,84 +2,13 @@
package Games::Word;
require Exporter;
@ISA = qw/Exporter/;
-@EXPORT_OK = qw/random_word is_word set_word_list
- random_permutation is_permutation/;
+@EXPORT_OK = qw/random_permutation is_permutation/;
use strict;
use warnings;
use Math::Combinatorics qw/factorial/;
use Test::Deep::NoTest;
-my $word_list = '';
-my $cache = 1;
-my %words = ();
-my @words = ();
-
-sub set_word_list {
- $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 %args = (cache => 1, @_);
- if ($args{cache}) {
- open my $fh, $word_list or die "Opening $word_list failed";
- for (<$fh>) {
- chomp;
- $words{$_} = 1;
- }
- @words = keys %words;
- $cache = 1;
- }
- else {
- $cache = 0;
- %words = @words = ();
- }
-
-}
-
-sub _random_word_cache {
- die "No words in word list" unless keys %words;
- return $words[int(rand(@words))];
-}
-
-sub _random_word_nocache {
- my $word;
-
- open my $fh, '<', $word_list or die "Opening $word_list failed";
- while (<$fh>) {
- $word = $_ if int(rand($.)) == 0;
- }
- chomp $word;
-
- return $word;
-}
-
-sub random_word {
- return _random_word_cache if $cache;
- return _random_word_nocache;
-}
-
-sub _is_word_cache {
- return $words{$_[0]};
-}
-
-sub _is_word_nocache {
- my $word = shift;
-
- open my $fh, '<', $word_list
- or die "Couldn't open word list: $word_list";
- while (<$fh>) {
- chomp;
- return 1 if $_ eq $word;
- }
-
- return 0;
-}
-
-sub is_word {
- return _is_word_cache(@_) if $cache;
- return _is_word_nocache(@_);
-}
-
sub random_permutation {
my $word = shift;
return '' if $word eq '';
diff --git a/lib/Games/Word/Wordlist.pm b/lib/Games/Word/Wordlist.pm
new file mode 100644
index 0000000..405cda6
--- /dev/null
+++ b/lib/Games/Word/Wordlist.pm
@@ -0,0 +1,159 @@
+#!perl
+package Games::Word::Wordlist;
+use strict;
+use warnings;
+
+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,
+ @_,
+
+ file => $word_list,
+ 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;
+ }
+ $self->{word_list} = [keys %{ $self->{word_hash} }];
+ }
+
+ bless $self, $class;
+ return $self;
+}
+
+sub _random_word_cache {
+ my $self = shift;
+ my @word_list = @{ $self->{word_list} };
+ die "No words in word list" unless @word_list;
+ return $word_list[int(rand(@word_list))];
+}
+
+sub _random_word_nocache {
+ my $self = shift;
+ my $word;
+
+ open my $fh, '<', $self->{file} or die "Opening $self->{file} failed";
+ while (<$fh>) {
+ $word = $_ if int(rand($.)) == 0;
+ }
+ chomp $word;
+
+ return $word;
+}
+
+sub random_word {
+ my $self = shift;
+ return $self->_random_word_cache if $self->{cache};
+ return $self->_random_word_nocache;
+}
+
+sub _is_word_cache {
+ my $self = shift;
+ return $self->{word_hash}{$_[0]};
+}
+
+sub _is_word_nocache {
+ my $self = shift;
+ my $word = shift;
+
+ open my $fh, '<', $self->{file}
+ or die "Couldn't open word list: $self->{file}";
+ while (<$fh>) {
+ chomp;
+ return 1 if $_ eq $word;
+ }
+
+ return 0;
+}
+
+sub is_word {
+ my $self = shift;
+ return $self->_is_word_cache(@_) if $self->{cache};
+ return $self->_is_word_nocache(@_);
+}
+
+=head1 NAME
+
+Games::Word::Wordlist - ???
+
+=head1 VERSION
+
+Version 0.01 released ???
+
+=cut
+
+our $VERSION = '0.01';
+
+=head1 SYNOPSIS
+
+ use Games::Word::Wordlist;
+ do_stuff();
+
+=head1 DESCRIPTION
+
+
+
+=head1 SEE ALSO
+
+L<Foo::Bar>
+
+=head1 AUTHOR
+
+Jesse Luehrs, C<< <jluehrs2 at uiuc.edu> >>
+
+=head1 BUGS
+
+No known bugs.
+
+Please report any bugs through RT: email
+C<bug-games-word at rt.cpan.org>, or browse
+L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Word>.
+
+=head1 SUPPORT
+
+You can find this documentation for this module with the perldoc command.
+
+ perldoc Games::Word
+
+You can also look for information at:
+
+=over 4
+
+=item * AnnoCPAN: Annotated CPAN documentation
+
+L<http://annocpan.org/dist/Games-Word>
+
+=item * CPAN Ratings
+
+L<http://cpanratings.perl.org/d/Games-Word>
+
+=item * RT: CPAN's request tracker
+
+L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-Word>
+
+=item * Search CPAN
+
+L<http://search.cpan.org/dist/Games-Word>
+
+=back
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2008 Jesse Luehrs.
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=cut
+
+1;
+