#!/usr/bin/perl #acro, a program to come up with a random acronym. #copyright 1999 by thomas smith. released under the GNU general public license. $version = "2.1.02"; # Wow, on the day after i release this, someone comes up with a use! random passwords! #thanks, carlos. srand(time ^ ($$ + ($$<<30))); # maximum and minimum number of characters to generate by default. $max = 7; $min = 3; # $letters is the number of letters in the acronym. $presetletters is set if an argument # was specified for the number of letters. $letters = $presetletters = 0; # acronym is uppercase (this is a default)? 1=yes, 0=no. $uppercase = 1; # $otherchars is set if the invoker supplied a character class to chose from. $otherchars = 0; # @goodchars is used if the invoker supplied a character class to chose from. it is an array # of characters to chose from. @goodchars = (); #debugging:$random = rand(127); print "Sample random number less than 127: $random\n"; # test for arguments. foreach $arg (@ARGV) { # go through the arguments. chomp $arg; if ($arg =~ /-h/i) { print "acro, by Thomas Smith, released under the GPL. Version $version.\n"; print "generates a random acronym, and prints it to STDOUT.\n"; print "INVOCATION:\n"; print "acro [ --help | --version ]\n"; print "acro [ - | number ] { [ --lowercase | -l ] | [ --uppercase | -u ] [ -nlines ]}\n"; exit 0; } elsif ($arg =~ /-v/i) { print "acro, by Thomas Smith, released under the GPL. Version $version.\n"; exit 0; } elsif ($arg =~ /^-u(.+)/i) { # this makes no sense unless the thing is a character class. # for instance, -u'[A-Za-z1380]' makes sense, but -u'hi' doesn't, since no one character # will match "hi". -uh does, if you want h to be the only character in your acronym. $re = $1; $otherchars = 1; } elsif ($arg =~ /-n\s*(\d+)/) { $howmanyacros = $1; } elsif ($arg =~ /-l/i) { $uppercase = 0; } elsif ($arg =~ /-u/i) { $uppercase = 1; } elsif ($arg =~ /^-$/) { $letters = ; if ($letters) { $presetletters = 1; } } elsif ($arg =~ /\d+/) { $presetletters = 1; $letters = $arg; } } # okay, now the actual program starts. if ($otherchars) { #set up the array of good characters, if we got the argument to use a custom set. for ($chrn = 0; $chrn < 128; $chrn += 1) { $curchr = chr($chrn); if ($curchr =~ /$re/) { push @goodchars, $curchr; } } } # newchar will find a new value for $char, one character. if (!$otherchars) { $newchar = sub { $charnum = int(rand(26)); # integer part of a random number, will be 0..25. # add 97 to it (a is ASCII 97). $charnum += 97; $char = chr($charnum); # convert number to character. if ($uppercase) { $char = uc($char); } return $char; } } else { $newchar = sub { $charnum = int(rand($#goodchars + 1)); $char = $goodchars[$charnum]; return $char; } } if (!defined($howmanyacros)) { $howmanyacros=1; } for ($acrosdone = 0; $acrosdone < $howmanyacros; $acrosdone = $acrosdone + 1) { if (!$presetletters) { #if the number of letters wasn't specified, make a random number. $letters = 0; #to eliminate spillover. until ($letters > $min) { $letters = rand($max); } } $acro = $char = ""; #debugging:print "generacting acronym, $letters letters long...\n"; $i = 1; # initialize i until ($i > $letters) { &$newchar; $acro .= $char; # append $char to $acro, which is "" to start with. #debugging:print "\n **** $acro\n"; $i += 1; } print "$acro\n"; } #debugging:print "for loop is done.\n";