Perl Programming
CIT 270L Integrative Programming
Assignment #1—Managing a User Account List
Due 2/20 in Lab section / Moodle
Objective: To manage a user list that can be modified and saved to a text file.
Inputs:
Input text file consisting of pairs of usernames and passwords, separated by a colon (:) without any spaces
User choice: (‘n’-new user account, ‘e’-edit existing user account, ‘d’- delete existing user account, ‘l’- list user accounts, ‘q’-quit)
Output:
List of user accounts, error messages when appropriate
Output text file consisting of pairs of username and passwords, separated by a colon (:) without any spaces
Specification: The program manages a list of user accounts and passwords supplied by a system administrator.
Any inputted username should be stripped of any non-alphanumeric characters
(special characters such as ! @ # $ % ^ & * ( ) _ + ; : ’ ”), using regular expression substitution:
$username =~ s/[^a-zA-Z0-9]//g;
and should be converted to lowercase: $username = lc($username);
Any inputted password should be stripped of an apostrophe (other symbols are allowed): $password =~ s/\’//g;
Each option should be implemented as a separate function within a Package that exports its functions, and the
functions are referenced by a hash indexed by the user input: my %function = (‘n’=>\&new_user, ‘e’=>\&edit_user, ‘d’=>\&delete_user,
‘l’=>\&list_accounts)
which can be called by assigning a variable to the key of the hash, such as:
my $selection = $function{$choice}
and updating the list stored as a hash as: %hash = $selection->(\%hash)
Within each function, access the actual parameter %hash as a local parameter using the shift command:
(eg: my $param = shift; my %local_hash = %$param) and update the hash according to the
function. The script file (.pl) should import the functions from the package (.pm) file to call the appropriate
function.
The program should loop until the user chooses to quit (selects status as ‘q’). If the user enters an illegal status,
the program will prompt again for the status input. Upon quitting, the program prompts the user to save the list to
a text file of the same name used to initially read the user list.
What to turn in:
A single zipped file (asmt1_yourlastname.zip) containing both source code files (the perl script called:
asmt1_yourlastname.pl, and the perl module file called: functs_yourlastname.pm) submitted via Moodle
(http://moodle.csun.edu) to the Lab section (not the lecture section). Any deviation from the format for
submission will result in an automatic -10%.
Sample Output: % perl asmt1_mcilhenny.pl
Enter file to open: myfile.txt
User accounts
-------------
n = New user account
e = Edit existing user account
d = Delete existing user account
l = List user accounts
q = Quit
Enter choice: n
Enter username: einstein
Enter password: e=mc^2
User accounts
-------------
n = New user account
e = Edit existing user account
d = Delete existing user account
l = List user accounts
q = Quit
Enter choice: n
Enter username: newton
Enter password: f=ma
User accounts
-------------
n = New user account
e = Edit existing user account
d = Delete existing user account
l = List user accounts
q = Quit
Enter choice: n
Enter username: pythagoras
Enter password: a^2+b^2=c^2
User accounts
-------------
n = New user account
e = Edit existing user account
d = Delete existing user account
l = List user accounts
q = Quit
Enter choice: n
Enter username: einstein
Username already exists!
User accounts
-------------
n = New user account
e = Edit existing user account
d = Delete existing user account
l = List user accounts
q = Quit
Enter choice: e
Enter username to modify: fibonacci
Username does not exist!
User accounts
-------------
n = New user account
e = Edit existing user account
d = Delete existing user account
l = List user accounts
q = Quit
Enter choice: e
Enter username to edit: newton
Enter current password: f=m*a
Incorrect password!
User accounts
-------------
n = New user account
e = Edit existing user account
d = Delete existing user account
l = List user accounts
q = Quit
Enter choice: e
Enter username to edit: newton
Enter current password: f=ma
Enter new password: force=mass*accelaration
User accounts
-------------
n = New user account
e = Edit existing user account
d = Delete existing user account
l = List user accounts
q = Quit
Enter choice: d
Enter username to delete: pythagoras
User removed
User accounts
-------------
n = New user account
e = Edit existing user account
d = Delete existing user account
l = List user accounts
q = Quit
Enter choice: l
einstein:e=mc^2
newton:force=mass*accelaration
User accounts
-------------
n = New user account
e = Edit existing user account
d = Delete existing user account
l = List user accounts
q = Quit
Enter choice: q
Save contents? (y/n): y
% more myfile.txt
einstein:e=mc^2
newton:force=mass*acceleration