Subject: | Wordlists in rand_words not cached |
In Data::Random::rand_words there's a section that checked whether the
wordlist is a reference. Presumably this is so that you can cache
wordlists. This section of code doesn't work.
It needs to test the reference type to be sure it isn't a file name
(SCALAR) and then bless the reference to a Data::Random::WordList
object like this:
# Check for a pre-existing wordlist object
if ( ref( $options{'wordlist'} ) ne 'SCALAR' ) {
$wl = $options{'wordlist'};
bless $wl, "Data::Random::WordList";
$close_wl = 0;
}
In addition to this, Data::Random::WordList should load its wordlist
into memory so it doesn't have to read from disk each time it's called.
Reading from disk is a big performance hit compared to reading from memory.
Since you're already reading the list to get the size, you can chomp
each line and push it onto a list reference that you bless onto the
class like the filehandle.
Here is an example of caching the list:
my $list;
# Calculate the number of lines in the file
my $size = 0;
while (<$fh>) {
chomp;
$list->[$size] = $_;
$size++;
}
# Create the object
my $self = bless {
'size' => $size,
'list' => $list,
}, $class;
Then instead of reading from the filehandle, you can reference directly
into the list. If you do that you don't need to keep the filehandle
open or bless it.