in reply to Hiding password on commandline

Term::ReadKey is what you want to use. Please read my previous node here on PerlMonks 224221 for a code example of how to do this. The example handles backspaces and rubbing out the characters back to whatever promt you use.

:-)
JamesNC

Replies are listed 'Best First'.
Re: Re: Hiding password on commandline
by Anonymous Monk on Jan 18, 2004 at 22:03 UTC
    I like your code, but I am on solaris, not win32. I used your while loop, but I kept getting asterisks even after I pressed return. In other words, the while loop never ended. Here's what I am using:

    print " Enter your password:\n"; ReadMode 3; while($key = ReadKey()){ $val = ord $key; push @password, $key unless $val == 8 || $val == 13; last if $val == 13; print "*" unless $val == 8; }
    By the way, what does the 8 and 13 correspond to? I am sure that may have something to do with the problem.
      Have you tried to press the backspace key? You code works, sure, if your user never makes a typing mistake. I didn't see any handling of backspaces in your code.

      Okay, I've learned that ord returns the numeric ascii value of the first character of $key. But where can I find a listing of those values so I can figure out what 8 and 13 correspond to? Do ascii values remain the same across platforms?
        Got it! The ascii value of eight is for backspaces. And 13 is for carriage returns, but on my solaris machine, 13 was not working, so I changed 13 to 10, the value for newlines, and it worked like a charm. Thanks.