Clare, I hope you've improved in the six years since you wrote the above.
Here's a version that isn't so newbie-ish.
#!/usr/bin/perl
use warnings;
use strict;
unless (@ARGV) {
print <<"END_HELP";
Clare's Random Password Generator
Usage: pwdgen template [number]
where template is a string composed of the following characters:
\tl\tlower case letter\n\tL\tupper case letter\n\tc\tlower case conson
+ant\n\tC\tupper case consonant
\tv\tlower case vowel\n\tV\tupper case vowel\n\tn\tnumber\n\tp\tpunctu
+ation mark\n\ta\tany character
Any character in the template which is not one of those above will be
+placed in the password
at the same position.
[number] refers to the number of passwords to generate - this defaults
+ to 10.
END_HELP
exit(0);
}
my $count;
my $template = $ARGV[0];
if ($ARGV[1] && $ARGV[1] =~ /^\d+$/) {
$count = $ARGV[1];
} else {
$count = 10;
}
my @numbers = (0..9);
my @punctuation = qw(. ? < > : @ / ! " % ^ & * ( ) - + = _);
my @loweralpha = ('a'..'z');
my @upperalpha = ('A'..'Z');
my @lowerconsonants = qw(b c d f g h j k l m n p q r s t v w x y z);
my @upperconsonants = qw(B C D F G H J K L M N P Q R S T V W X Y Z);
my @lowervowels = qw(a e i o u);
my @uppervowels = qw(A E I O U);
my @all = (@loweralpha, @upperalpha, @numbers, @punctuatio
+n);
my $symbols = {
'n' => \@numbers,
'p' => \@punctuation,
'l' => \@loweralpha,
'L' => \@upperalpha,
'c' => \@lowerconsonants,
'C' => \@upperconsonants,
'v' => \@lowervowels,
'V' => \@uppervowels,
'a' => \@all,
};
for (0..$count) {
generate_password();
}
sub generate_password {
my $password = '';
$password =
join '',
map {
exists $symbols->{$_}
? $symbols->{$_}[rand @{$symbols->{$_}
+}]
: $_
}
split //, $template;
print "$password\n";
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|