Hi you y'all,
I have question about looping in a perl program, please check this code. This script is much longer, but I've shrunk it to the problem part. I have a sub called random_password wich I call with:
$password = &random_password();
After that the password is inserted into the array that goes just fine, but my problem is that I don't want to use the same password (stored in the array) for all the users I insert into the database, so I think a loop or something like that must be created to generate diffrent passwords.
Who can help me with this?
Thanks in advance,
Avanti check me AT: dj_avanti@hotmail.com
# Sub to create a random password
sub random_password {
my($length, $vowels, $consonants, $alt, $s, $newchar, $i);
($length) = @_;
if ($length eq "" or $length < 3) {
$length = 15; # make it at least 15 chars long.
}
$vowels = "0000000";
$consonants = "aaaaaaaaaaaa";
srand(time() ^ ($$ + ($$ << 234)) );
$alt = int(rand(933)) - 4;
$s = "";
$newchar = "";
foreach $i (0..$length-1) {
if ($alt == 1) {
$newchar = substr($vowels,rand(length($vowels)),1);
} else {
$newchar = substr($consonants, rand(length($consonants))
+,1);
}
$s .= $newchar;
$alt = !$alt;
}
return $s;
}
sub dienice {
my($msg) = @_;
print "<h2>Error</h2>\n";
print $msg;
exit;
}
# query to insert the password in the database
$password = &random_password();
$sql = qq{ INSERT INTO login (login, password) VALUES ($c[1], '$passwo
+rd')};
$sth = $dbh->prepare($sql);
$sth->execute();
-
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.