#!/usr/bin/perl -w ############################################################################# # # Program: # randword # Author: # Christian J. Robinson # Copyright: # GPL version 2 # Purpose: # Print random words from a dictionary file, or a random characters. # Assumptions: # - That /usr/share/dict/words or /usr/dict/words exists # Exit values: # This script currently will always exit with a success value unless # there's an invocation usage problem # Known Limitations: # - Data::Random is capable of random dates/times, but this script doesn't # implement the feature. # TODO: # ############################################################################# # # $Id: randword,v 1.3 2008/03/04 04:09:40 infynity Exp $ # # $Log: randword,v $ # Revision 1.3 2008/03/04 04:09:40 infynity # *** empty log message *** # # Revision 1.2 2008/03/04 04:08:38 infynity # *** empty log message *** # # Revision 1.1 2008/03/04 04:00:45 infynity # Initial revision # ############################################################################# use 5.008; use strict; use Getopt::Long; use File::Basename; use Data::Random qw/rand_words rand_set/; sub usage($); my $basename = basename($0); my $version = (split(' ', '$Revision: 1.3 $'))[1]; my $copyright = <<'EOF'; Copyright March, 2008 by Christian J. Robinson Distributable under the terms of the GPL public license, version 2. EOF my $count = 1; my $words = '/usr/share/dict/words'; $words = '/usr/dict/words' unless (-e $words); my %args; Getopt::Long::config qw/bundling/; GetOptions(\%args, 'sort|s', 'characters|c:s' => sub{ $args{'characters'} = $_[1] ? $_[1] : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'; }, 'dictionary|d=s' => \$words, 'version|V' => sub{ my $v = "$basename version $version"; print "$v\n$copyright"; exit 0; }, 'help|h' => sub{usage(0);}, ) or usage(1); if (defined $ARGV[0]) { if ($ARGV[0] =~ m/^[0-9]+$/) { $count = $ARGV[0]; } else { warn "Count argument must be a positive integer\n"; usage(1); } } my @words; if ($args{'characters'}) { my @chars = split('', $args{'characters'}); @words = rand_set( set => \@chars, size => $count, ); } else { @words = rand_words( size => $count, wordlist => $words, ); } @words = sort {lc($a) cmp lc($b)} @words if $args{'sort'}; print join("\n", @words) . "\n"; sub usage($) { my $rval = shift; my ($where, $out); if ($rval) { $where = *STDERR; } else { $where = *STDOUT; } $out = <<"EOF"; Usage: $basename [options] [count] Prints [count] random words from the dictonary. The default for [count] is $count. Options: --sort, -s Sort the output, case insensitive --dictonary, -d Set as the dictionary, the format is one word per line --characters, -c [characters] Instead of words from the dictonary, print [count] [characters], default is alphanumeric characters --version, -V Print version and copyright and exit --help, -h Show this usage statement and exit EOF print $where $out; exit $rval; }