in reply to Re: Recovering From Win32::Console
in thread Recovering From Win32::Console
Term::ReadKeywouldn't load on my system. Something about missing terminators or something.
Anyway, here's the code:
#!/usr/bin/perl use strict; use Win32::Console; my $usrnam = get_username(); my $usrpwd = get_password(); while (1) { my $inpflg = 0; my $geninp; while (!$inpflg) { print "Command: "; chomp ($geninp = <STDIN>); my $tstinp = $geninp; $tstinp =~ s/^\s+//g; $tstinp =~ s/\s+$//g; if ($tstinp ne '') { $inpflg = 1; } } if ($geninp =~ /exit/) { exit; } elsif ($geninp =~ /go/) { print "Username: $usrnam\n"; print "Password: $usrpwd\m"; } } sub getpass() { # Establish a console control object my $cnsbuf; # our $cnsbuf; if (!$cnsbuf) { $cnsbuf = new Win32::Console(STD_INPUT_HANDLE); } if (!$cnsbuf) { return(undef); } # Ensure console ECHO is OFF my $oldmod = $cnsbuf->Mode(); if ($oldmod & ENABLE_ECHO_INPUT) { my $newmod = ($oldmod ^ ENABLE_ECHO_INPUT); $cnsbuf->Mode($newmod); } # Go get the password my $usrpas = <STDIN>; chomp($usrpas); # Return console to old mode $cnsbuf->Mode($oldmod); # Print the missing newline print "\n"; # Give 'em what they asked for return($usrpas); } sub get_username() { print "Username: "; chomp (my $wrknam = <STDIN>); return $wrknam; } sub get_password() { print "Password: "; chomp (my $wrkpas = &getpass()); return $wrkpas; }
If you change the "my $cnsbuf;"to "our $cnsbuf;"the wild-n-crazy console problem will go away.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Recovering From Win32::Console
by BrowserUk (Patriarch) on Jun 05, 2003 at 22:58 UTC |