TheYoungMonk has asked for the wisdom of the Perl Monks concerning the following question:

hi monks! i need to accept a password string from the user in such a way that it is not shown on console when he is typing.

Tried using Term::ReadKey and setting Readmode('noecho'), but gave an error message saying "Can't locate Term/ReadKey.pm", I'm using PERL version 5.6.

Could someone point out what is the problem and how to rectify?

Please suggest any other way to do the same, without depending on Term::ReadKey.

Replies are listed 'Best First'.
Re: Accepting Password from user
by Trimbach (Curate) on Jan 23, 2003 at 16:21 UTC
    It looks like Term::ReadKey isn't installed on your machine. Installing it is pretty easy though, using either the excellent CPAN module (if you're on Unix/Linux) or ppm (if you using Activestate Perl on Windows). Either way check out this tutorial.

    Gary Blackburn
    Trained Killer

Re: Accepting Password from user
by hardburn (Abbot) on Jan 23, 2003 at 16:26 UTC

    Here is a subroutine for just that purpose. In this case, it asks for a password twice and returns undef if they didn't match.

    sub get_passwd_on_console { use POSIX; print "Password: "; # Set the terminal to "no echo" mode for reading the passphrase my ($term, $oterm, $echo, $noecho, $fd_stdin); $fd_stdin = fileno(STDIN); $term = POSIX::Termios->new; $term->getattr($fd_stdin); $oterm = $term->getlflag(); $echo = ECHO | ECHOK | ICANON; $noecho = $oterm & ~$echo; $term->setlflag($noecho); $term->setattr($fd_stdin, TCSANOW); my $passwd = <>; chomp $passwd; print "\n"; print "Password (retype): "; my $passwd2 = <>; chomp $passwd2; print "\n"; # Set the terminal back $term->setlflag($echo); $term->setattr($fd_stdin, TCSANOW); return ($passwd eq $passwd2) ? $passwd : undef; }
      Thanks for all ur suggestions...unfortunately my problem remains unsolved!!

      Currently I need to use Perl 5.6 on a Windows 2000 machine. For the code given by hardburn, it says Posix::termios not supported!

      i am unable to use CPAN module termkey and not able to install it using CPAN or PPM...Maybe because I'm not using ActiveState perl

      So..is there any other suggestions which can be used to solve this problem of accepting user password without printing on console ?? If perl 5.6 doesnt provide any ways by default, anyother subroutine code that'll do the job of accepting pswd without console display ?

      Still awaiting enlightening suggestions from the monks...

Re: Accepting Password from user
by hotshot (Prior) on Jan 23, 2003 at 16:43 UTC
    You should install Term::ReadKey moudule from CPAN.
    Do the followings from your prompt:
    prompt> perl -MCPAN -eshell cpan>install Term::ReadKey


    Hotshot
Re: Accepting Password from user
by Mr. Muskrat (Canon) on Jan 23, 2003 at 16:36 UTC
    Let me guess. You saw how to use Term::ReadKey in perldoc perlfaq8 and it told you to:
    use Term::ReadKey; ReadMode('noecho'); $password = ReadLine(0);

    But you did not pay attention to the line above it...
    You can also do this for most systems using the Term::ReadKey module from CPAN, which is easier to use and in theory more portable.

    Have you tried installing the CPAN module Term::ReadKey? It is not hard.