use strict; use warnings; my $desired_length = 8; my $selected_word; my $candidate_count = 0; open my $dict_fh, '<', '/usr/share/dict/words' or die "Can't read '/usr/share/dict/words': $!\n"; WORD: while ( my $word = <$dict_fh> ) { chomp $word; next WORD if length $word != $desired_length; $selected_word = $word if rand ++$candidate_count < 1; } print "Selected '$selected_word' out of $candidate_count candidates.\n";