我有要求用户输入密码的Perl脚本。我怎么也只能随声附和“*”代替字符的用户类型,因为他们键入?

我使用的是Windows XP / Vista操作系统。

有帮助吗?

解决方案

您可以用期限玩:: ReadKey。这是一个很简单的例子,一些检测退格键和删除键。我测试了它在Mac OS X 10.5,但根据 ReadKey手动它要在Windows下运行。的手册表明Windows下使用非阻塞读(ReadKey(-1))将失败。这就是为什么我使用ReadKey(0)谁基本上getc(更多的的 libc的手册)。

#!/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";   

其他提示

在过去我已经使用 IO ::提示获得这一点。

use IO::Prompt;
my $password = prompt('Password:', -e => '*');
print "$password\n";

如果你不希望使用任何套餐......仅用于UNIX

system('stty','-echo');
chop($password=<STDIN>);
system('stty','echo');

您应该看一看在任期限:: ReadKey 或的的Win32 ::控制台。可以使用这些模块来读取单个关键笔划和发射“*”或whathever。

在Pierr吕克的计划大厦,刚添加的反斜杠一些控制。有了这个,你不能永远保持按下反斜杠:

sub passwordDisplay() {
    my $password = "";
    # Start reading the keys
    ReadMode(4); #Disable the control keys
    my $count = 0;
    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
                    if ($count > 0) {
                            $count--;
                            #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) {
                    $count++;
                    $password = $password.$key;
                    print "*";
            }
    }
    ReadMode(0); #Reset the terminal once we are done
    return $password;
}

使用Pierr吕克的程序

# Start reading the keys                                                                                                                                                                                       
ReadMode(4); #Disable the control keys                                                                                                                                                                         
while(ord($key = ReadKey(0)) != '13' )                                                                                                                                                                            
# 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 && (length($password) > 0)) {                                                                                                                                                                   
        # 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) {                                                                                                                                                                                  
        $password = $password.$key;                                                                                                                                                                            
        print "*";                                                                                                                                                                              
    }                                                                                                                                                                                                         
}                                                                                                                                                                                                              
ReadMode(0); #Reset the terminal once we are done

你试过存储字符串(让你的程序仍然可以读取它),并找出它的长度,然后创建相同长度的字符串,但只能用“*”?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top