Advertisement
Guest User

Untitled

a guest
Oct 16th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. use strict;
  2. use lib './';
  3. use functs_skeleton;
  4.  
  5. # create a hash of functions--see asmt sheet
  6. my %function = ( '1' => \&add_user, '2' => \&edit_user, '3' => \&delete_user, '4' => \&print_list);
  7. #global var declarations go here
  8. my $fh;
  9. my $fn;
  10. my %hash;
  11. #Open file for reading
  12. # prompt user for filename & store in variable:"$fn" : print, <>, chomp
  13. print "Enter Filename: ";
  14. $fn = <>;
  15. chomp($fn);
  16.  
  17. open ($fh, "<", $fn);
  18. # if file does not exist, create it by opening an empty file for writing
  19. if (!$fh) {
  20. open ($fh, ">", $fn);
  21. }
  22. #transfer file line by line into hash with a while loop, splitting the username and password by the delimiter character ":"
  23. while (my $line = <$fh>) {
  24. my @line_array = split(/:/,$line);
  25. my $username = $line_array[0];
  26. my $password = $line_array[1];
  27. chomp($password);
  28. $hash{$username} = $password;
  29. }
  30. # outer loop until user quits
  31. outer:{
  32. while(1) {
  33. #inner loop until user enters correct option
  34. my $func = "UNSET";
  35. my $choice;
  36. inner:{
  37. while(1) {
  38. # print menu & prompt/chomp for user input: $choice
  39. print "\n\nChoose an option:\n\t1. Add user account\n\t2. Edit existing user account\n\t3. Delete existing user account\n\t4. Print list of user accounts\n\t5. Save and quit\n\nEnter choice: ";
  40. $choice = <>;
  41. chomp($choice);
  42. if ($choice eq '5') {last outer;}
  43. # if the user enters a valid choice, exit inner loop
  44. # if ($choice eq '1') {$func=\&add_user;last;}
  45. # if ($choice eq '2') {$func=\&edit_user;last;}
  46. # if ($choice eq '3') {$func=\&delete_user;last;}
  47. # if ($choice eq '4') {$func=\&print_list;last;}
  48. #print"choice='".$choice."', val='".$function{$choice}."'\n";
  49. if (exists($function{$choice})) {last;}
  50. print "Unknown choice\n";
  51. } #end of the inner loop
  52. }
  53. # set local variable to appropriate function references in hash by user's choice & call appropriate function by passing in the hash as a parameter
  54. # and assigning the function result to the hash to update it
  55. # print"choice=".$choice."\n";
  56. %hash = $function{$choice}->(\%hash);
  57.  
  58. } # end of outer loop
  59. }
  60.  
  61. #Close the file for reading
  62. close($fh);
  63. # prompt user to save changes, if so, then print the entire contents of the hash to the file & close the file for writing
  64. print "Save (y/n)?";
  65. my $saveit = <>;
  66. chomp($saveit);
  67. if ($saveit eq 'y') {
  68. open( $fh, ">", $fn);
  69. #for every entry in the hash, write it to the file
  70. foreach my $key (keys %hash) {
  71. print $fh $key . ":" . $hash{$key} . "\n";
  72. }
  73. close($fh);
  74. } # if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement