如何使用 Perl 输入密码并将字符替换为“*"? [英] How can I enter a password using Perl and replace the characters with '*'?

查看:24
本文介绍了如何使用 Perl 输入密码并将字符替换为“*"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求用户输入密码的 Perl 脚本.我怎样才能在用户输入字符时只回显 '*' 来代替他们输入的字符?

I have a Perl script that requires the user to enter a password. How can I echo only '*' in place of the character that the user types, as they type it?

我使用的是 Windows XP/Vista.

I'm using Windows XP/Vista.

推荐答案

您可以使用 Term::ReadKey.这是一个非常简单的示例,其中包含对退格键和删除键的一些检测.我已经在 Mac OS X 10.5 上对其进行了测试,但根据 ReadKey 手册应该在 Windows 下工作.手册 指出在 Windows 下使用非阻塞读取(ReadKey(-1)) 将失败.这就是为什么我使用基本上是 getc 的 ReadKey(0) (更多关于 getc 在 libc 手册).

You can play with Term::ReadKey. Here is a very simple example, with some detection for backspace and delete key. I've tested it on Mac OS X 10.5 but according to the ReadKey manual it should work under Windows. The manual indicates that under Windows using non-blocking reads (ReadKey(-1)) will fail. That's why I'm using ReadKey(0) who's basically getc (more on getc in the libc manual).

#!/usr/bin/perl                                                                                                                                                                                                

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

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

print "
Please 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 " ";                                                                                                                                                                                         
    } 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 "

Your super secret password is: $password
";   

这篇关于如何使用 Perl 输入密码并将字符替换为“*"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆