Darian Horn wrote:

> Hello All, 
> 
> I am having trouble figuring this out. Maybe one of you can help. I am trying 
> to use the Win32::Console module in a function call, but when the function 
> exits the Console object gets deleted and my application looses its handle. 
> Does anyone know how to use this without this side effect? Some example code 
> below. 
> 
> Thanks, 
> dhorn
> 
> # Start Code
> 
> use Win32::Console;
> use strict;
> 
> my $pass = &getUserPass;
> print "Pass: $pass\n";
> 
> print "Enter something: ";
> my $text = <STDIN>; # <- Does not grab standard input. 
> print "TEXT: $text\n";
> 
> sub getUserPass {
>   my $stdin = new Win32::Console(STD_INPUT_HANDLE); 
>   my $pwd = ""; 
>   $stdin->Mode(ENABLE_PROCESSED_INPUT); 
>   local $| = 1;  
>   print "Enter Password: "; 
>   while (my $input = $stdin->InputChar(1)) { 
>     if("\r" eq $input ) { 
>       last; 
>     } elsif ("\b" eq $input ) { 
>       if( "" ne chop( $pwd )) { 
>         print "\b \b"; 
>       } 
>       next; 
>     } 
>     $pwd .=$input; 
>     print "*"; 
>   } 
> 
>   print "\r" . " " x (40) . "\r";
>   return $pwd;
> } 
> 
> # End code

I'm not sure what the bug is with Console.pm, but you could
try Term::ReadKey - it should work for you:

use strict;
use warnings;
use Term::ReadKey;      END { ReadMode ('restore'); }   # just in case

my $pwd = getUserPass ();
print "Password entered was $pwd\n";
print "Type <CR> to continue:\n";
my $text = <STDIN>;
print "TEXT: $text\n";

sub getUserPass {

my $pwd = '';
binmode STDIN;
print "Password: ";
ReadMode ('cbreak');
while (defined (my $ch = ReadKey ())) { # -1=non-blocking read; >0 = sec. timed

        # echoes * for each char and allows line correction

        last if $ch eq "\x0a" or $ch eq "\x0d";
        if ($ch eq "\x08") {    # backspace
                print "\b \b" if $pwd;          # back up 1
                chop $pwd;
                next;
        }
        if ($ch eq "\x15") {    # ^U
                print "\b \b" x length $pwd;    # back 1 for each char
                $pwd = '';
                next;
        }
        $pwd .= $ch;
        print '*';
}
print "\n";
ReadMode ('restore');
return $pwd;

}

__END__
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to