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

Hey Guys, Well im trying to create a change password script. Ive got a password.txt file of which holds the username and passwords eg.
jrojas:jrsWIWyBMGLdM test:teH0wLIpW0gyQ peter:pe9Wcpzwb4XmA
But within the script i obvioulsy want to check it is that user, so im getting them to input their password, of which it then encrypts to check it against the passwords.txt file.. BUT only the first line of my passwords.txt file is being read by the looks of it.. I can update the user thats on the top of the file, but no others..
print ("Enter Your Current Password:"); system ("stty -echo"); $inputline3 = <STDIN>; system ("stty echo"); chop ($inputline3); #Encrypt the password $salt = $userName2; $encryptpw2 = crypt ($inputline3,$salt); open my $fh, "<", 'passwords.txt' or die "Can't open password file: $! +"; while (<$fh>) { chomp; ($name,$passwd) = split(/:/); if ($encryptpw2 eq $passwd) { print ("\nEnter Your new password:"); system ("stty -echo"); $newpass = <STDIN>; system ("stty echo"); chop ($newpass); print ("\nPlease enter your new password in again:"); system ("stty -echo"); $newpass2 = <STDIN>; system ("stty echo"); chop ($newpass2); } else { die ("\n\nYour Passwords Did Not Match, Please Try running the + script again. \n\n"); }
Can someone please, please help me!?

Replies are listed 'Best First'.
Re: File input not working properly?
by hipowls (Curate) on Apr 22, 2008 at 06:38 UTC

    • Your script check equality of passwords, not username password pairs.
    • If the given password doesn't match the password in the first line then it dies
    Your loop needs to be more like
    while ( my $line = <$fh> ) { chomp $line; my ( $name, $passwd ) = split /:/, $line; next unless $name eq $userName; .... }

    You mak also want to look at Term::ReadKey or IO::Prompt for the password prompt

Re: File input not working properly?
by apl (Monsignor) on Apr 22, 2008 at 09:36 UTC
    Following up on what hipowls said, you might want to load the passwords into a hash (keyed on username) before you prompt the user.

    That way, if you later change your progam to give the user (say) three chances to specify the password, you won't have to read through the file three time.