Dominic said:
Bonjour,
Does anybody have a way to read a password, from a user, and only
display "****" for each character read?!?
Thanks for any idea!
PS: I want to do that (via a Perl script) on Linux and Windows.
Bonjour,
Maybe the following sample will give you some ideas. You still have to
test it on Linux and Windows because I did not do that for you.
Listen to gurus who might to review/critique this quick hack. The
script is by no mean fully tested and foolproof.
Let us know if works for you!
MrReallyVeryNice
use strict;
use warnings;
use Term::ReadKey;
my $number_attempt=0;
my $password1 = hide_password($number_attempt);
my $max_number_attempt=3;
my $password2 = hide_password($number_attempt);
while ($number_attempt<$max_number_attempt+1)
{
if ($password1 ne $password2)
{
$password2 = hide_password($number_attempt);
}
else
{
last;
}
}
if ($password1 ne $password2)
{
print "\nYou failed to confirm your password\n";
}
else
{
# you most likely want to remove the print statement below and
# do whatever processing you need to do with the confirmed password
print "\nYour password was confirmed\n";
}
sub hide_password {
$|=1;
my $password = '';
my $key;
if ($_[0] == 0)
{
print "Enter password:\n";
}
else
{
print "\nConfirm password:\n";
}
while(1)
{
while(not defined($key = ReadKey(-1))) {};
if(ord($key) != 13)
{
if(ord($key) == 8)
{
print "\b\0\b";
chop($password);
}
else
{
$password .= $key;
print "*";
}
}
else {last;}
}
$|=0;
$number_attempt++;
return $password;
}
__END__