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

OK, So ive gone through and been trying to clean up that part of the script a bit more. But i found out what im having troubles with in regards to updating.. I am able to update the user thats on the first line of the passwords.txt file.. But i cant to any of the others! Therefore its only reading the first line? What can i do to change this?

#!/usr/bin/perl -w print "\n\n\n\n\n\n\n------------------------------------------------- +---------------------"; print "\nWelcome to Jamie's User Management System. \n\nPlease select +the option that you wish to use:\n -a -- To create a new username and + password.\n -u -- To update a users password.\n -d -- To delete a us +er from the system.\n -h -- For more help!\n\n -q -- To quit this sys +tem\n\n "; # Take initial input from user print "\nPlease input your desired first Option --> : "; chomp ($option = <>); # #Determine what option is selected # Add in a new user # if ($option eq "-a") { print "\n\n\n--- Add User Menu --- \n"; print "\nEnter User Name --> : "; chomp ($userName = <>); open my $fh, "<", 'passwords.txt' or die "Can't open password file: $! +"; while (<$fh>) { chomp; ($name,$passwd) = split(/:/); if ($userName eq $name) { print "Sorry That Username already Exists"; print "\nEnter User Name --> : "; chomp ($userName = <>); } } print ("Enter a secret password:"); system ("stty -echo"); $inputline1 = <STDIN>; system ("stty echo"); chop ($inputline1); print ("\nPlease enter your password in again:"); system ("stty -echo"); $inputline2 = <STDIN>; system ("stty echo"); chop ($inputline2); #Verify that password has been entered correctly $outputline = $inputline1 eq $inputline2 ? "\n\nYes, that is the correct password!\n" : "\n\nNo, that is not the correct password.\n"; print ($outputline); #Encrypt the password $salt = $userName; $encryptpw = crypt ($inputline2,$salt); #Write username and password to the passwords.txt file open (FILE, ">>passwords.txt"); print FILE "$userName:$encryptpw\n"; close (FILE); print ("Your Username and Password have been saved!\n"); # Take input from user print "\nPlease input your next Option ( -u,-d,-h,-q ) --> : "; chomp ($option = <>); } # # Update existing user # if ($option eq "-u") { print "\n\n\n--- Update User Password Menu --- \n"; print "\nEnter Your Current User Name --> : "; chomp ($userName2 = <STDIN>); print ("Enter Your Current Password:"); system ("stty -echo"); $inputline3 = <STDIN>; system ("stty echo"); chop ($inputline3); #Encrypt the password $salt = $userName2; $encryptpw2 = crypt ($inputline3,$salt); print "$encryptpw2\n"; open my $fh, "<", 'passwords.txt' or die "Can't open password file: $! +"; while (<$fh>) { chomp; ($name,$passwd) = split(/:/); print "$passwd\n"; if ($encryptpw2 eq $passwd) { print ("\nPlease Enter 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); } elsif ($encryptpw2 ne $passwd) { die ("\n\nYour Passwords Did Not Match, Please Try running the + script again. \n\n"); } if ($newpass eq $newpass2) { #Encrypt the password $salt2 = $userName2; $encryptpw3 = crypt ($newpass2,$salt2); use Tie::File; my $file_name = 'passwords.txt'; tie @array, 'Tie::File', $file_name or die; for (@array) { s/$inputline3/$encryptpw3/g; print ("\n\nYour Username and Password have been updated and s +aved!\n\n"); } untie @array; } elsif ($newpass ne $newpass2) { die ("\n\nYour Passwords Did Not Match, Please Try again. \n\n"); } } # Take input from user print "\nPlease input your next Option ( -u,-d,-h,-q ) --> : "; chomp ($option = <>); }

Replies are listed 'Best First'.
Re: User management system: update problem
by moritz (Cardinal) on Apr 17, 2008 at 09:07 UTC
    $fh =~ s/$inputline3/$encryptpw3/g;

    This line doesn't do anything useful. $fh is a file handle, not a string - you can't substitute anything in a file handle.

    And even if you could, the file is opened for reading only.

    Use Tie::File if you want read- and write access to a file at the same time.

Re: User management system: update problem
by GrandFather (Saint) on Apr 17, 2008 at 08:56 UTC

    Try adding strictures (use strict; use warnings;) to your code and see what errors you can turn up for yourself.


    Perl is environmentally friendly - it saves trees
Re: User management system: update problem
by andreas1234567 (Vicar) on Apr 17, 2008 at 09:11 UTC
    Reduce your problem as much as you can, e.g.
    $ echo -e "old\nfarts\nsmell" > foo $ cat foo old farts smell $ perl use strict; use warnings; my $fh = undef; open ($fh, "<", 'foo') or die "Can't open: $!"; while (<$fh>) { chomp; $fh =~ s/old/new/g; } close ($fh) or die "Can't close: $!"; __END__ $ cat foo old farts smell
    The file was not updated, which means your use of the replace operator on the filehandle probably doesn't work as you expect.

    Also, consider using the readmore tags since your code is extensive. See Perl Monks Approved HTML tags.

    --
    When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do. [1]
Re: User management system: update problem
by mscharrer (Hermit) on Apr 17, 2008 at 09:08 UTC
    Hi, just had a quick look over it. Is there a reason why you sometimes use <> and sometimes <STDIN> to read in? You should know that <> is not exactly the same as <STDIN> because it will also try to read from filenames given in @ARGV, but I'm not sure what order right now. Anyway a s/<>/<STDIN>/ might make your code more consistent, and reduce the chance for buggy behavior if someone calls your script with arguments.

    Martin

      and some other general tips:

      You can write your beginning lines like this:

      print "\n"x7, '-'x80, "\n"; print "Welcome to Jamie's User Management System. \n\n" . "Please select the option that you wish to use:\n" . " -a -- To create a new username and password.\n" . " -u -- To update a users password.\n" . " -d -- To delete a user from the system.\n" . " -h -- For more help!\n\n" . " -q -- To quit this system\n\n";
      The usage of the program 'perltidy' (from inside vim with :%!perltidy) can improve the readability of your scripts. I'm using it a lot.
Re: User management system: update problem
by Aim9b (Monk) on Apr 17, 2008 at 10:49 UTC
    Have you tried just updating the file "in the clear", without the encryption, to see if the update is really where the problem lies?