| 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
|
|
4
|
use strict; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
30
|
|
|
16
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
29
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
435
|
use FileHandle; |
|
|
1
|
|
|
|
|
1856
|
|
|
|
1
|
|
|
|
|
3
|
|
|
19
|
1
|
|
|
1
|
|
284
|
use File::Basename qw(dirname); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
340
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
################################################################################ |
|
22
|
|
|
|
|
|
|
# - Global Constants and Variables |
|
23
|
|
|
|
|
|
|
################################################################################ |
|
24
|
|
|
|
|
|
|
$Data::Random::WordList::VERSION = '0.11_3'; |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
################################################################################ |
|
27
|
|
|
|
|
|
|
# - Subroutines |
|
28
|
|
|
|
|
|
|
################################################################################ |
|
29
|
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
################################################################################ |
|
31
|
|
|
|
|
|
|
# new() |
|
32
|
|
|
|
|
|
|
################################################################################ |
|
33
|
|
|
|
|
|
|
sub new { |
|
34
|
130
|
|
|
130
|
1
|
111
|
my $proto = shift; |
|
35
|
130
|
|
|
|
|
165
|
my %options = @_; |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# Check if what was passed in was a prototype reference or a class name |
|
38
|
130
|
|
33
|
|
|
293
|
my $class = ref($proto) || $proto; |
|
39
|
|
|
|
|
|
|
|
|
40
|
130
|
|
33
|
|
|
180
|
$options{'wordlist'} ||= dirname($INC{'Data/Random.pm'}).'/Random/dict'; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# Create a new filehandle object |
|
43
|
130
|
50
|
|
|
|
329
|
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
|
|
|
|
|
5794
|
my $size = 0; |
|
48
|
130
|
|
|
|
|
826
|
while (<$fh>) { |
|
49
|
3380
|
|
|
|
|
4178
|
$size++; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Create the object |
|
53
|
130
|
|
|
|
|
380
|
my $self = bless { |
|
54
|
|
|
|
|
|
|
'fh' => $fh, |
|
55
|
|
|
|
|
|
|
'size' => $size, |
|
56
|
|
|
|
|
|
|
}, $class; |
|
57
|
|
|
|
|
|
|
|
|
58
|
130
|
|
|
|
|
312
|
return $self; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
################################################################################ |
|
62
|
|
|
|
|
|
|
# close() |
|
63
|
|
|
|
|
|
|
################################################################################ |
|
64
|
|
|
|
|
|
|
sub close { |
|
65
|
130
|
|
|
130
|
1
|
103
|
my $self = shift; |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Close the filehandle |
|
68
|
130
|
|
|
|
|
256
|
$self->{'fh'}->close; |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
################################################################################ |
|
72
|
|
|
|
|
|
|
# get_words() |
|
73
|
|
|
|
|
|
|
################################################################################ |
|
74
|
|
|
|
|
|
|
sub get_words { |
|
75
|
130
|
|
|
130
|
1
|
102
|
my $self = shift; |
|
76
|
130
|
|
50
|
|
|
197
|
my $num = shift || 1; |
|
77
|
|
|
|
|
|
|
|
|
78
|
130
|
|
|
|
|
144
|
my $fh = $self->{'fh'}; |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
# Perform some error checking |
|
81
|
130
|
50
|
33
|
|
|
435
|
die 'the size value must be a positive integer' |
|
82
|
|
|
|
|
|
|
if $num < 0 || $num != int($num); |
|
83
|
130
|
50
|
|
|
|
173
|
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
|
|
|
|
|
172
|
my %rand_lines = (); |
|
89
|
130
|
|
|
|
|
201
|
for ( my $i = 0 ; $i < $num ; $i++ ) { |
|
90
|
1262
|
|
|
|
|
744
|
my $rand_line; |
|
91
|
|
|
|
|
|
|
|
|
92
|
1262
|
|
|
|
|
782
|
do { |
|
93
|
2433
|
|
|
|
|
3773
|
$rand_line = int( rand( $self->{'size'} ) ); |
|
94
|
|
|
|
|
|
|
} while ( exists( $rand_lines{$rand_line} ) ); |
|
95
|
|
|
|
|
|
|
|
|
96
|
1262
|
|
|
|
|
2049
|
$rand_lines{$rand_line} = 1; |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
130
|
|
|
|
|
90
|
my $line = 0; |
|
100
|
130
|
|
|
|
|
144
|
my @rand_words = (); |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Seek to the beginning of the filehandle |
|
103
|
130
|
50
|
|
|
|
294
|
$fh->seek( 0, 0 ) or die "could not seek to position 0 in wordlist: $!"; |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# Now get the lines |
|
106
|
130
|
|
|
|
|
1154
|
while (<$fh>) { |
|
107
|
3380
|
|
|
|
|
2018
|
chomp; |
|
108
|
3380
|
100
|
|
|
|
4607
|
push ( @rand_words, $_ ) if $rand_lines{$line}; |
|
109
|
|
|
|
|
|
|
|
|
110
|
3380
|
|
|
|
|
4501
|
$line++; |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Return an array or an array reference, depending on the context in which the sub was called |
|
114
|
130
|
50
|
|
|
|
175
|
if ( wantarray() ) { |
|
115
|
0
|
|
|
|
|
0
|
return @rand_words; |
|
116
|
|
|
|
|
|
|
} |
|
117
|
|
|
|
|
|
|
else { |
|
118
|
130
|
|
|
|
|
539
|
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.11_3 |
|
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 |