gpt4 book ai didi

perl - 如何使用Perl输入密码,并用'*'替换字符?

转载 作者:行者123 更新时间:2023-12-03 13:06:23 36 4
gpt4 key购买 nike

我有一个Perl脚本,要求用户输入密码。当用户键入字符时,我该如何仅回声“ *”代替用户键入的字符?

我正在使用Windows XP / Vista。

最佳答案

您可以玩Term :: ReadKey。这是一个非常简单的示例,其中对退格键和删除键进行了一些检测。我已经在Mac OS X 10.5上对其进行了测试,但是根据ReadKey manual,它应该可以在Windows下运行。 manual表示在Windows下使用非阻塞读取(ReadKey(-1))将失败。这就是为什么我使用基本上是getc的ReadKey(0)(有关libc manual中的getc的更多信息)的原因。

#!/usr/bin/perl                                                                                                                                                                                                

use strict;
use warnings;
use Term::ReadKey;

my $key = 0;
my $password = "";

print "\nPlease input your password: ";

# 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) {
# DEL/Backspace was pressed
#1. Remove the last char from the password
chop($password);
#2 move the cursor back by one, print a blank character, move the cursor back by one
print "\b \b";
} elsif(ord($key) < 32) {
# Do nothing with these control characters
} else {
$password = $password.$key;
print "*(".ord($key).")";
}
}
ReadMode(0); #Reset the terminal once we are done
print "\n\nYour super secret password is: $password\n";

关于perl - 如何使用Perl输入密码,并用'*'替换字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/701078/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com