Hiding User Input

别等时光非礼了梦想. 提交于 2019-12-04 04:28:28
use Term::ReadKey;
print "Please enter your artifactory user name:";
$username = <STDIN>;
chomp($username);
ReadMode('noecho'); # don't echo
print "Please enter your artifactory password:";
$password = <STDIN>;
chomp($password);
ReadMode(0); #back to normal
print "\n\n";

I would try outputting the environment variables (%ENV) during the sessions that work, and then again during the sessions that don't. I find that, when dealing with terminal IO, you have to carefully tweak the "TERM" variable based on things like the $^O variable and $ENV{SESSIONNAME} (in Windows).

drew

how about Term::ReadKey's ReadMode(4)? i've just used this in a personal project, having found the answer here

works on cygwin / win7, can't vouch for native windows shell however.

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

sub get_input {
  my $key = 0;
  my $user_input = "";

 # disable control keys and start reading keys until enter key is pressed (ascii 10)
 ReadMode(4);
 while (ord($key = ReadKey(0)) != 10)
   {
     if (ord($key) == 127 || ord($key) == 8) {
       # backspace / del was pressed.  remove last char and move cursor back one space.
       chop ($user_input);
       print "\b \b";
     } elsif (ord($key) < 32) {
         # control characters, do nothing
     } else {
         $user_input = $user_input . $key;
         print "*";
     }
   }
  ReadMode(0);
  return $user_input;
}

# variables
my $password = "";
my $username = "";

print "\nPlease input your username: ";
$username = get_input();
print "\nHi, $username\n";

print "\nPlease input your password: ";
$password = get_input();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!