fcd15e0b |
1 | #! /usr/bin/perl |
2 | |
3 | use Catacomb qw(:random); |
4 | use Getopt::Long qw(:config gnu_getopt); |
5 | |
6 | $help = 0; |
7 | $dict = undef; |
8 | GetOptions("dict=s" => \$dict, "help" => \$help) or exit 1; |
9 | |
10 | DICT: { |
11 | last DICT if defined $dict; |
12 | $dict = $ENV{"DICT"}; last DICT if defined $dict; |
13 | $dict = "/usr/share/dict/words"; last DICT if -r $dict; |
14 | -r $dict or $dict = "/usr/dict/words"; last DICT if -r $dict; |
15 | die "no appropriate word list"; |
16 | } |
17 | open DICT, "$dict" or die "can't open $dict: $!"; |
18 | |
19 | sub filter { 1; } |
20 | $filter = shift(); |
21 | if (defined $filter) { |
22 | eval "sub filter { $filter };"; |
23 | die $@ if $@; |
24 | } |
25 | |
26 | $rng = Catacomb::Rand::Counter->new("blowfish", $random->fill(32)); |
27 | |
28 | $word = undef; |
29 | $i = 1; |
30 | WORD: while (<DICT>) { |
31 | chomp(); |
32 | next WORD unless filter; |
33 | $rng->range($i) == 0 and $word = $_; |
34 | $i++; |
35 | } |
36 | close DICT; |
37 | print "$word\n"; |
38 | |