Tuesday, August 05, 2025

readpw (password input prompt, displaying ** when typing)

How Can We Help?

< Back
You are here:
Print
use Term::ReadKey;

 

sub readpwd {
#
# Get the variables
#--
($prompt)=@_;
$key=0;
$password="";
#
print $prompt . " : ";
# Start reading the keys
ReadMode(4); #Disable the control keys
while(ord($key = ReadKey(0)) != 10)
# This will continue until the Enter key is pressed (decimal value of 10)
{
# For all value of ord($key) see http://www.asciitable.com/
if(ord($key) == 127 || ord($key) == 8) {
chop($password); print "\b \b";
} elsif(ord($key) < 32) {
# Do nothing with these control characters
} else {
$password = $password.$key;
print "*";
}
}
print "\n";
ReadMode(0); #Reset the terminal once we are done
return $password;
}

 

Table of Contents
Back To Top