Much wider support for Catacomb in all its glory.
[catacomb-perl] / random-word.pl
diff --git a/random-word.pl b/random-word.pl
new file mode 100755 (executable)
index 0000000..7e87bda
--- /dev/null
@@ -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 (<DICT>) {
+  chomp();
+  next WORD unless filter;
+  $rng->range($i) == 0 and $word = $_;
+  $i++;
+}
+close DICT;
+print "$word\n";
+