Manjula Babu wrote:

> hi all,
> i have a code snippet as below:
> 
> while( $pass1 ne $pass2 ) {
>       while( $pass1 eq "" || $pass1 !~ /^[a-zA-Z0-9-_]{3,16}$/ ) {
>         print "Enter a password for the administrator account: ";
>         $pass1 = <STDIN>;
>         chomp $pass1;
>         print "Hello\n";
>         print " You entered $pass1 \n";
>         if(! $pass1 ) {
>           print "\n\nIt's just plain stupid to not have a password.  Try again!\n";
>         } elsif ( $pass1 !~ /^.{3,16}$/ ) {
>           print "The password must be 3-16 characters in length.";
>         }
>       }
>       print "\nPlease retype the password to verify: ";
>       $pass2 = <STDIN>;
>       chomp $pass2;
>       if ($pass1 ne $pass2) {
>         print "\n\nPasswords don't match.  Try again!\n";
>         $pass1 = "";
>         $pass2 = "*";
>       }
>     }
> 
> although i enter the password as "Hello", it does not come out of the inner loop. I 
> guess it is because pass1 reads even carriage return which is probably invalid 
> character. Can anyone help me to handle this.

This should work, but you should use Term::ReadKey to stop the echo and
not echo it out yourself (example snippet after).

use strict;

my $pass1 = '';
my $pass2 = '';
while (1) {

        while ($pass1 eq '' || $pass1 !~ /^[\w-]{3,16}$/ ) {

                print "Enter a password for the administrator account: ";
                $pass1 = <STDIN>;
                chomp $pass1;
                print "You entered $pass1\n";
                if (!$pass1) {
                        print "It's plain stupid to not have a password.\n";
                        print "Try again!\n";
                } elsif ($pass1 !~ /^.{3,16}$/) {
                        print "The password must be 3-16 characters.";
                }
        }
        print "Please retype the password to verify: ";
        $pass2 = <STDIN>;
        chomp $pass2;
        last if $pass1 eq $pass2;
        print "Passwords don't match.  Try again!\n";
        $pass1 = '';
        $pass2 = '';
}

__END__


Example to read password with no echo:

        use Term::ReadKey;

        binmode STDIN;
        print "Password: ";
        ReadMode ('cbreak');
        while (defined (my $ch = ReadKey ())) {

                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 '*';
        }
        ReadMode ('restore');


-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (Free site for Perl/Lakers)

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

Reply via email to