File Coverage

blib/lib/Data/Random/WordList.pm
Criterion Covered Total %
statement 44 45 97.7
branch 7 12 58.3
condition 4 11 36.3
subroutine 7 7 100.0
pod 3 3 100.0
total 65 78 83.3


line stmt bran cond sub pod time code
1             ################################################################################
2             # Data::Random
3             #
4             # A module used to generate random data.
5             #
6             # Author: Adekunle Olonoh
7             # Date: October 2000
8             ################################################################################
9              
10             package Data::Random::WordList;
11              
12             ################################################################################
13             # - Modules and Libraries
14             ################################################################################
15 1     1   5 use strict;
  1         2  
  1         40  
16 1     1   3 use warnings;
  1         2  
  1         29  
17              
18 1     1   495 use FileHandle;
  1         2218  
  1         6  
19 1     1   315 use File::Basename qw(dirname);
  1         2  
  1         469  
20              
21             ################################################################################
22             # - Global Constants and Variables
23             ################################################################################
24             $Data::Random::WordList::VERSION = '0.12';
25              
26             ################################################################################
27             # - Subroutines
28             ################################################################################
29              
30             ################################################################################
31             # new()
32             ################################################################################
33             sub new {
34 130     130 1 134 my $proto = shift;
35 130         201 my %options = @_;
36              
37             # Check if what was passed in was a prototype reference or a class name
38 130   33     338 my $class = ref($proto) || $proto;
39              
40 130   33     232 $options{'wordlist'} ||= dirname($INC{'Data/Random.pm'}).'/Random/dict';
41              
42             # Create a new filehandle object
43 130 50       406 my $fh = new FileHandle $options{'wordlist'}
44             or die "could not open $options{'wordlist'} : $!";
45              
46             # Calculate the number of lines in the file
47 130         7261 my $size = 0;
48 130         1059 while (<$fh>) {
49 3380         4546 $size++;
50             }
51              
52             # Create the object
53 130         524 my $self = bless {
54             'fh' => $fh,
55             'size' => $size,
56             }, $class;
57              
58 130         383 return $self;
59             }
60              
61             ################################################################################
62             # close()
63             ################################################################################
64             sub close {
65 130     130 1 130 my $self = shift;
66              
67             # Close the filehandle
68 130         300 $self->{'fh'}->close;
69             }
70              
71             ################################################################################
72             # get_words()
73             ################################################################################
74             sub get_words {
75 130     130 1 128 my $self = shift;
76 130   50     204 my $num = shift || 1;
77              
78 130         146 my $fh = $self->{'fh'};
79              
80             # Perform some error checking
81 130 50 33     498 die 'the size value must be a positive integer'
82             if $num < 0 || $num != int($num);
83 130 50       228 die
84             "$num words were requested but only $self->{'size'} words exist in the wordlist"
85             if $num > $self->{'size'};
86              
87             # Pick which lines we want
88 130         155 my %rand_lines = ();
89 130         207 for ( my $i = 0 ; $i < $num ; $i++ ) {
90 1300         855 my $rand_line;
91              
92 1300         893 do {
93 2554         4370 $rand_line = int( rand( $self->{'size'} ) );
94             } while ( exists( $rand_lines{$rand_line} ) );
95              
96 1300         2512 $rand_lines{$rand_line} = 1;
97             }
98              
99 130         120 my $line = 0;
100 130         146 my @rand_words = ();
101              
102             # Seek to the beginning of the filehandle
103 130 50       380 $fh->seek( 0, 0 ) or die "could not seek to position 0 in wordlist: $!";
104              
105             # Now get the lines
106 130         1405 while (<$fh>) {
107 3380         2292 chomp;
108 3380 100       5031 push ( @rand_words, $_ ) if $rand_lines{$line};
109              
110 3380         4912 $line++;
111             }
112              
113             # Return an array or an array reference, depending on the context in which the sub was called
114 130 50       199 if ( wantarray() ) {
115 0         0 return @rand_words;
116             }
117             else {
118 130         634 return \@rand_words;
119             }
120             }
121              
122             1;
123              
124              
125              
126             =head1 NAME
127              
128             Data::Random::WordList - Perl module to get random words from a word list
129              
130              
131             =head1 SYNOPSIS
132              
133             use Data::Random::WordList;
134              
135             my $wl = new Data::Random::WordList( wordlist => '/usr/dict/words' );
136              
137             my @rand_words = $wl->get_words(10);
138              
139             $wl->close();
140              
141              
142             =head1 DESCRIPTION
143              
144             Data::Random::WordList is a module that manages a file containing a list of words.
145              
146             The module expects each line of the word list file to contain only one word. It could thus be easily used to select random lines from a file, but for coherency's sake, I'll keep referring to each line as a word.
147              
148             The module uses a persistent filehandle so that there isn't a lot of overhead every time you want to fetch a list of random words. However, it's much more efficient to grab multiple words at a time than it is to fetch one word at a time multiple times.
149              
150             The module also refrains from reading the whole file into memory, so it can be safer to use with larger files.
151              
152              
153             =head1 METHODS
154              
155             =head2 new()
156              
157             Returns a reference to a new Data::Random::WordList object. Use the "wordlist" param to initialize the object:
158              
159             =over 4
160              
161             =item *
162              
163             wordlist - the path to the wordlist file. If a path isn't supplied, the wordlist distributed with this module is used.
164              
165             =back
166              
167             =head2 get_words([NUM])
168              
169             NUM contains the number of words you want from the wordlist. NUM defaults to 1 if it's not specified. get_words() dies if NUM is greater than the number of words in the wordlist. This function returns an array or an array reference depending on the context in which it's called.
170              
171             =head2 close()
172              
173             Closes the filehandle associated with the word list. It's good practice to do this every time you're done with the word list.
174              
175              
176             =head1 VERSION
177              
178             0.12
179              
180              
181             =head1 AUTHOR
182              
183             Originally written by: Adekunle Olonoh
184              
185             Currently maintained by: Buddy Burden (barefoot@cpan.org), starting with version 0.06
186              
187              
188             =head1 COPYRIGHT
189              
190             Copyright (c) 2000-2011 Adekunle Olonoh.
191             Copyright (c) 2011-2015 Buddy Burden.
192             All rights reserved. This program is free software; you
193             can redistribute it and/or modify it under the same terms as Perl itself.
194              
195              
196             =head1 SEE ALSO
197              
198             L
199              
200             =cut