代码之家  ›  专栏  ›  技术社区  ›  Matthew Farwell

如何使用Perl输入密码并将字符替换为“*”?

  •  18
  • Matthew Farwell  · 技术社区  · 17 年前

    我使用的是Windows XP/Vista。

    7 回复  |  直到 17 年前
        1
  •  24
  •   Peter Stuifzand    17 年前

    过去我用过 IO::Prompt 为了这个。

    use IO::Prompt;
    my $password = prompt('Password:', -e => '*');
    print "$password\n";
    
        2
  •  19
  •   user270160 user270160    16 年前

    如果你不想使用任何软件包。..仅适用于UNIX

    system('stty','-echo');
    chop($password=<STDIN>);
    system('stty','echo');
    
        3
  •  17
  •   Pierre-Luc Simard    16 年前

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

    #!/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";   
    
        4
  •  8
  •   innaM    17 年前

    你应该看看 Term::ReadKey Win32::Console 。你可以使用这些模块来读取单个按键并发出“*”或其他什么。

        5
  •  1
  •   Hany    14 年前

    在Pier Luc的程序的基础上,增加了一些对睫毛的控制。有了这个,你就不能永远按反斜杠了:

    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;
    }
    
        6
  •  0
  •   Muhammad Hamayoon    7 年前

    使用Pier Luc的程序

    # 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
    
        7
  •  -4
  •   The Sheek Geek    17 年前

    你是否尝试过存储字符串(这样你的程序仍然可以读取它)并找出它的长度,然后创建一个相同长度的字符串,但只使用“*”?