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         1  
  1         25  
16 1     1   4 use warnings;
  1         1  
  1         22  
17              
18 1     1   402 use FileHandle;
  1         1936  
  1         5  
19 1     1   279 use File::Basename qw(dirname);
  1         2  
  1         748  
20              
21             ################################################################################
22             # - Global Constants and Variables
23             ################################################################################
24             our $VERSION = '0.13';
25              
26             ################################################################################
27             # - Subroutines
28             ################################################################################
29              
30             ################################################################################
31             # new()
32             ################################################################################
33             sub new {
34 130     130 1 200 my $proto = shift;
35 130         273 my %options = @_;
36              
37             # Check if what was passed in was a prototype reference or a class name
38 130   33     364 my $class = ref($proto) || $proto;
39              
40 130   33     231 $options{'wordlist'} ||= dirname($INC{'Data/Random.pm'}).'/Random/dict';
41              
42             # Create a new filehandle object
43 130 50       475 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         8803 my $size = 0;
48 130         1538 while (<$fh>) {
49 3380         6394 $size++;
50             }
51              
52             # Create the object
53 130         814 my $self = bless {
54             'fh' => $fh,
55             'size' => $size,
56             }, $class;
57              
58 130         447 return $self;
59             }
60              
61             ################################################################################
62             # close()
63             ################################################################################
64             sub close {
65 130     130 1 197 my $self = shift;
66              
67             # Close the filehandle
68 130         360 $self->{'fh'}->close;
69             }
70              
71             ################################################################################
72             # get_words()
73             ################################################################################
74             sub get_words {
75 130     130 1 197 my $self = shift;
76 130   50     236 my $num = shift || 1;
77              
78 130         209 my $fh = $self->{'fh'};
79              
80             # Perform some error checking
81 130 50 33     440 die 'the size value must be a positive integer'
82             if $num < 0 || $num != int($num);
83             die
84             "$num words were requested but only $self->{'size'} words exist in the wordlist"
85 130 50       237 if $num > $self->{'size'};
86              
87             # Pick which lines we want
88 130         196 my %rand_lines = ();
89 130         250 for ( my $i = 0 ; $i < $num ; $i++ ) {
90 1312         1546 my $rand_line;
91              
92             do {
93 2587         4906 $rand_line = int( rand( $self->{'size'} ) );
94 1312         1439 } while ( exists( $rand_lines{$rand_line} ) );
95              
96 1312         2679 $rand_lines{$rand_line} = 1;
97             }
98              
99 130         192 my $line = 0;
100 130         188 my @rand_words = ();
101              
102             # Seek to the beginning of the filehandle
103 130 50       363 $fh->seek( 0, 0 ) or die "could not seek to position 0 in wordlist: $!";
104              
105             # Now get the lines
106 130         2668 while (<$fh>) {
107 3380         4990 chomp;
108 3380 100       15196 push ( @rand_words, $_ ) if $rand_lines{$line};
109              
110 3380         6814 $line++;
111             }
112              
113             # Return an array or an array reference, depending on the context in which the sub was called
114 130 50       322 if ( wantarray() ) {
115 0         0 return @rand_words;
116             }
117             else {
118 130         720 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