Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. package functs_skeleton;
  2.  
  3. use strict;
  4.  
  5. # Export functions--copy next 4 lines verbatim
  6. use Exporter;
  7. use vars qw (@ISA @EXPORT);
  8. @ISA = qw(Exporter);
  9. @EXPORT = ("add_user", "edit_user", "delete_user", "print_list");
  10.  
  11.  
  12. #################### add_user function ####################
  13. sub add_user {
  14. my $param = shift;
  15. my %local_hash = %$param;
  16.  
  17. #prompt/chomp for username & password
  18. print "Username: ";
  19. my $username = <>;
  20. chomp($username);
  21. $username =~ s/[^a-zA-Z0-9]//g;
  22. $username = lc($username);
  23. print "Password for user `".$username."`: ";
  24. my $password = <>;
  25. chomp($password);
  26.  
  27.  
  28. $password =~ s/\'//g;
  29. $password = reverse($password);
  30.  
  31. if (exists($local_hash{$username})){
  32. print "ERROR: USERNAME TAKEN\n";
  33. return %local_hash;
  34. }
  35.  
  36. $local_hash{$username} = $password;
  37.  
  38. # strip off special chars from username (ref asmt sheet)
  39. # convert username to lowercase (ref asmt sheet)
  40. # strip off apostrophe from password (ref asmt sheet)
  41. # encrypt password
  42.  
  43. # check if username already exists & if so exit, else assign password as value to the hash referenced by username as the key
  44.  
  45. # assign password as value to the hash referenced by username
  46. print "User `".$username."` created.\n";
  47.  
  48. return %local_hash;
  49. }
  50.  
  51.  
  52.  
  53. #################### edit_user function ####################
  54. sub edit_user {
  55. my $param = shift;
  56. my %local_hash = %$param;
  57.  
  58. print "Username to Edit: ";
  59. my $username = <>;
  60. chomp($username);
  61. $username =~ s/[^a-zA-Z0-9]//g;
  62. $username = lc($username);
  63. print "Password for user `".$username."`: ";
  64. my $password = <>;
  65. chomp($password);
  66.  
  67.  
  68. $password =~ s/\'//g;
  69. $password = reverse($password);
  70.  
  71. print "New password for user `".$username."`:";
  72. my $new_password = <>;
  73. chomp($new_password);
  74.  
  75. $new_password =~ s/\'//g;
  76. $new_password = reverse($new_password);
  77.  
  78. if (!exists($local_hash{$username})){
  79. print"ERROR: USERNAME NOT FOUND\n";
  80. return %local_hash;
  81. }
  82.  
  83. if (!($local_hash{$username} eq $password)){
  84. print"ERROR: BAD PASSWORD\n";
  85. return %local_hash;
  86. }
  87.  
  88. $local_hash{$username} = $new_password;
  89.  
  90. # prompt/chomp for username
  91. # strip off special chars from username (ref asmt sheet)
  92. # convert username to lowercase (ref asmt sheet)
  93. # check if username DOESNT already exists & if NOT so exit
  94.  
  95. # prompt/chomp for current password
  96. # strip off apostrophe from password (ref asmt sheet)
  97. # encrypt password
  98. # check if password DOESNT exist & if NOT so exit
  99.  
  100. #prompt/chomp for new password
  101. # strip off apostrophe from password (ref asmt sheet)
  102.  
  103. # assign password as value to the hash referenced by username as the key
  104. print "User `".$username."` password updated.\n";
  105. return %local_hash;
  106. }
  107.  
  108.  
  109.  
  110. #################### delete_user function ####################
  111. sub delete_user {
  112. my $param = shift;
  113. my %local_hash = %$param;
  114.  
  115. # prompt/chomp for username
  116. # strip off special chars from username (ref asmt sheet)
  117. # convert username to lowercase (ref asmt sheet)
  118. # check if username DOESNT already exists & if NOT so exit
  119.  
  120. # prompt/chomp for password
  121. # strip off apostrophe from password (ref asmt sheet)
  122. # encrypt password
  123. # check if password DOESNT exist & if NOT so exit
  124.  
  125. print "Username to Delete: ";
  126. my $username = <>;
  127. chomp($username);
  128. $username =~ s/[^a-zA-Z0-9]//g;
  129. $username = lc($username);
  130. print "Password for user `".$username."`: ";
  131. my $password = <>;
  132. chomp($password);
  133.  
  134.  
  135. $password =~ s/\'//g;
  136. $password = reverse($password);
  137.  
  138. if (!exists($local_hash{$username})){
  139. print"ERROR: USERNAME NOT FOUND\n";
  140. return %local_hash;
  141. }
  142.  
  143. if (!($local_hash{$username} eq $password)){
  144. print"ERROR: BAD PASSWORD\n";
  145. return %local_hash;
  146. }
  147.  
  148.  
  149.  
  150. #delete key from hash (ref slides)
  151. delete $local_hash{$username};
  152. print"User `".$username."` deleted.\n";
  153. return %local_hash;
  154. }
  155.  
  156.  
  157. #################### print_list function ####################
  158. sub print_list {
  159. my $param = shift;
  160. my %local_hash = %$param;
  161.  
  162. # print out to the screen each key & value pair from hash (ref .pl file when the hash is wriiten to a file)
  163.  
  164. foreach my $key (keys %local_hash) {
  165. print $key . ":" .reverse($local_hash{$key}) . "\n";
  166. }
  167.  
  168. return %local_hash;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement