Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package functs_skeleton;
- use strict;
- # Export functions--copy next 4 lines verbatim
- use Exporter;
- use vars qw (@ISA @EXPORT);
- @ISA = qw(Exporter);
- @EXPORT = ("add_user", "edit_user", "delete_user", "print_list");
- #################### add_user function ####################
- sub add_user {
- my $param = shift;
- my %local_hash = %$param;
- #prompt/chomp for username & password
- print "Username: ";
- my $username = <>;
- chomp($username);
- $username =~ s/[^a-zA-Z0-9]//g;
- $username = lc($username);
- print "Password for user `".$username."`: ";
- my $password = <>;
- chomp($password);
- $password =~ s/\'//g;
- $password = reverse($password);
- if (exists($local_hash{$username})){
- print "ERROR: USERNAME TAKEN\n";
- return %local_hash;
- }
- $local_hash{$username} = $password;
- # strip off special chars from username (ref asmt sheet)
- # convert username to lowercase (ref asmt sheet)
- # strip off apostrophe from password (ref asmt sheet)
- # encrypt password
- # check if username already exists & if so exit, else assign password as value to the hash referenced by username as the key
- # assign password as value to the hash referenced by username
- print "User `".$username."` created.\n";
- return %local_hash;
- }
- #################### edit_user function ####################
- sub edit_user {
- my $param = shift;
- my %local_hash = %$param;
- print "Username to Edit: ";
- my $username = <>;
- chomp($username);
- $username =~ s/[^a-zA-Z0-9]//g;
- $username = lc($username);
- print "Password for user `".$username."`: ";
- my $password = <>;
- chomp($password);
- $password =~ s/\'//g;
- $password = reverse($password);
- print "New password for user `".$username."`:";
- my $new_password = <>;
- chomp($new_password);
- $new_password =~ s/\'//g;
- $new_password = reverse($new_password);
- if (!exists($local_hash{$username})){
- print"ERROR: USERNAME NOT FOUND\n";
- return %local_hash;
- }
- if (!($local_hash{$username} eq $password)){
- print"ERROR: BAD PASSWORD\n";
- return %local_hash;
- }
- $local_hash{$username} = $new_password;
- # prompt/chomp for username
- # strip off special chars from username (ref asmt sheet)
- # convert username to lowercase (ref asmt sheet)
- # check if username DOESNT already exists & if NOT so exit
- # prompt/chomp for current password
- # strip off apostrophe from password (ref asmt sheet)
- # encrypt password
- # check if password DOESNT exist & if NOT so exit
- #prompt/chomp for new password
- # strip off apostrophe from password (ref asmt sheet)
- # assign password as value to the hash referenced by username as the key
- print "User `".$username."` password updated.\n";
- return %local_hash;
- }
- #################### delete_user function ####################
- sub delete_user {
- my $param = shift;
- my %local_hash = %$param;
- # prompt/chomp for username
- # strip off special chars from username (ref asmt sheet)
- # convert username to lowercase (ref asmt sheet)
- # check if username DOESNT already exists & if NOT so exit
- # prompt/chomp for password
- # strip off apostrophe from password (ref asmt sheet)
- # encrypt password
- # check if password DOESNT exist & if NOT so exit
- print "Username to Delete: ";
- my $username = <>;
- chomp($username);
- $username =~ s/[^a-zA-Z0-9]//g;
- $username = lc($username);
- print "Password for user `".$username."`: ";
- my $password = <>;
- chomp($password);
- $password =~ s/\'//g;
- $password = reverse($password);
- if (!exists($local_hash{$username})){
- print"ERROR: USERNAME NOT FOUND\n";
- return %local_hash;
- }
- if (!($local_hash{$username} eq $password)){
- print"ERROR: BAD PASSWORD\n";
- return %local_hash;
- }
- #delete key from hash (ref slides)
- delete $local_hash{$username};
- print"User `".$username."` deleted.\n";
- return %local_hash;
- }
- #################### print_list function ####################
- sub print_list {
- my $param = shift;
- my %local_hash = %$param;
- # print out to the screen each key & value pair from hash (ref .pl file when the hash is wriiten to a file)
- foreach my $key (keys %local_hash) {
- print $key . ":" .reverse($local_hash{$key}) . "\n";
- }
- return %local_hash;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement