X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-perl/blobdiff_plain/f9952aec1cf6c64a5681308eea817b6113a37433..fcd15e0b7a3d0f0ca2f30953573f8d1f6b8e8bd2:/random-word.pl diff --git a/random-word.pl b/random-word.pl new file mode 100755 index 0000000..7e87bda --- /dev/null +++ b/random-word.pl @@ -0,0 +1,38 @@ +#! /usr/bin/perl + +use Catacomb qw(:random); +use Getopt::Long qw(:config gnu_getopt); + +$help = 0; +$dict = undef; +GetOptions("dict=s" => \$dict, "help" => \$help) or exit 1; + +DICT: { + last DICT if defined $dict; + $dict = $ENV{"DICT"}; last DICT if defined $dict; + $dict = "/usr/share/dict/words"; last DICT if -r $dict; + -r $dict or $dict = "/usr/dict/words"; last DICT if -r $dict; + die "no appropriate word list"; +} +open DICT, "$dict" or die "can't open $dict: $!"; + +sub filter { 1; } +$filter = shift(); +if (defined $filter) { + eval "sub filter { $filter };"; + die $@ if $@; +} + +$rng = Catacomb::Rand::Counter->new("blowfish", $random->fill(32)); + +$word = undef; +$i = 1; +WORD: while () { + chomp(); + next WORD unless filter; + $rng->range($i) == 0 and $word = $_; + $i++; +} +close DICT; +print "$word\n"; +