Pet*_*and 24
在过去,我使用了IO :: Prompt.
use IO::Prompt;
my $password = prompt('Password:', -e => '*');
print "$password\n";
Run Code Online (Sandbox Code Playgroud)
小智 18
如果您不想使用任何包...仅适用于UNIX
system('stty','-echo');
chop($password=<STDIN>);
system('stty','echo');
Run Code Online (Sandbox Code Playgroud)
Pie*_*ard 16
您可以使用Term :: ReadKey.这是一个非常简单的示例,有一些检测退格键和删除键.我在Mac OS X 10.5上测试了它,但根据ReadKey手册,它应该在Windows下运行.该手册指出,在Windows下使用非阻塞读(ReadKey(-1))会失败.这就是为什么我使用基本上的ReadKey(0)getc(更多关于libc手册中的 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";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33743 次 |
| 最近记录: |