如何提示终端用户输入密码而不在终端中显示密码? How can I enter a password using Perl and replace the characters with '*'? 有一个类似的问题,但答案似乎已过时或已损坏。
我一直在寻找这个问题的答案,并且对后面几页结果中的问题不满意,这些问题的答案与其他问题无关。
另外,perlfaq8有一个不完整的例子(哦,谁曾经维护过;)。
use Term::ReadKey;
ReadMode('noecho');
my $password = ReadLine(0);
当您在不重置终端的情况下执行此操作时,没有其他任何回应!哎呀!
我也在寻找更新的答案或模块,我们可以提供给想要这样做的人。
最佳答案
使用 Term::ReadKey ,这有效:
sub prompt_for_password {
require Term::ReadKey;
# Tell the terminal not to show the typed chars
Term::ReadKey::ReadMode('noecho');
print "Type in your secret password: ";
my $password = Term::ReadKey::ReadLine(0);
# Rest the terminal to what it was previously doing
Term::ReadKey::ReadMode('restore');
# The one you typed didn't echo!
print "\n";
# get rid of that pesky line ending (and works on Windows)
$password =~ s/\R\z//;
# say "Password was <$password>"; # check what you are doing :)
return $password;
}
关于perl - Perl 如何在不显示终端的情况下提示输入密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39801195/