in reply to Hiding password on commandline

Here is the code I use for such a purpose. It doesn't output asterisks, instead it outputs nothing at all to the screen and captures the keystrokes ... very similar to how Unix does it for logins.

Enjoy!
Dean

use Term::ReadKey; print "\nEnter your password: "; ReadMode 'noecho'; $pass = ReadLine 0; chomp $pass; ReadMode 'normal'; print $pass,"\n";

Replies are listed 'Best First'.
Re: Re: Hiding password on commandline
by rinceWind (Monsignor) on Jan 19, 2004 at 12:08 UTC
    This is very similar to a question in perlfaq8, including the example code.

    However, reading the POD for Term::ReadKey, it says for ReadLine categorically:

    This call is currently not available under Windows.
    Curious... I wonder why not. Maybe it is and the POD is out of date. However, rolling some code to handle backspaces or <DEL>s by hand, or using Term::ReadLine to prompt and read the line, will give a truly portable solution.

    --
    I'm Not Just Another Perl Hacker

    Update: Corrected a typo. I was referring to the POD in Term::ReadKey, not Term::ReadLine - sorry for the confusion.

Re: Re: Hiding password on commandline
by isotope (Deacon) on Jan 20, 2004 at 02:35 UTC
    Be sure to reverse the print and noecho:
    ReadMode 'noecho'; print "\nEnter your password: ";
    For typical uses, the difference isn't noticeable, but if you'd ever used Expect, you'd be quick to make that change. Don't prompt for the password until you've ensured it won't echo, or there's the possibility that the password will be entered (and echoed) before you disable echo.


    --isotope