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

Hello, I have an program that needs an userid and password. Then it tests an ftp connection. My question is I got the follow ing code from Mastering Perl5 by Eric Herrmann. What the code is doing is doubling the letters I am entering in the password. Two things can this code be fixed or does someone have an better way of doing it using Win32? The code to get password:
$console=Win32::Console->new(STD_INPUT_HANDLE); print<<"eof"; Enter Your novell password Crtl + U to erase line, Crtl + H or backspace to delete an character Carriage return (Enter key) to end entry eof GETPASSWORD: while (($char = ($console->Input())[5]) != $CARRIAGE_RETURN ) { SWITCH:{ #test for crtl h if ($char == 8){ print "\b \b"; chop $password; last SWITCH; } # test for crtl u if ($char == 21){ $currentInputSize = length($password); print "\b \b" x $currentInputSize; last SWITCH; } #Carriage Return if ($char == 13){ if (length ($password) == 0){ print "PASSWORD REQUIRED \n"; exit; } # stop entry, exit loop last GETPASSWORD; } #Strip out extra stuff if ($char >= 0x21 && $char <= 0x7E){ #convert number to ASCII int $password .= chr $char; print "*"; last SWITCH; } # must call Console::Input twice $char = ($console->Input())[5]; } #end get password
So if the password is watch it prints out wwaattcchh. Any help would be welcomed. Hal.

Replies are listed 'Best First'.
Re: Getting input for an password.
by moodster (Hermit) on May 22, 2002 at 11:13 UTC
    Instead of doing the terminal IO by hand, you might want to check out the CPAN module Term::ReadKey; which grants you more control over what gets printed to the screen. To read a line from the terminal without having it printed, use the ReadMode function (and remember to reset it before the program terminates, otherwise you screw up the display).

    Cheers,
    --Moodster