in reply to Looking for pointers or optimizations.
In addition to the previous comments,
my $words_file = @ARGV;You assign an array to a scalar. Array is treated in scalar context, and you have only number of arguments in it. Use this instead: my ($words_file) = @ARGV;. In this case an anonymous array gets assigned, and nobody gets harmed.
You read STDIN, not the $fh. Use <$fh>, not just <>. Also, in array context the <> operator returns the array with file contents. You can just run my @words = <$fh>;. See readline for more.open (my $fh, ">", $words_file); my @words; while (<>) { push(@words, $_); }
my $word = lc($words[rand($words) + 1]);scalar @words returns count of the words; rand returns any number between (including) 0 and specified number (not including). You should also round this value using int: my $word = lc $words[int rand($words+1)]; Edit: my @words = <$fh>; # not $fh
Sorry if my advice was wrong.
|
---|
In Section
Seekers of Perl Wisdom