# initialize variables # Loop # Display menu and choose item # Process menu item: # a: add son/father pair # d: delete son/father pair # e: exit # g: get a father # o: output hash neatly # r: replace a father # x: get a grandfather #### initialize_variables(); my $is_time_to_exit = 0; while (! $is_time_to_exit) { my $choice = choose_menu_item(); if ($choice eq 'a') { add_son_father_pair(); } elsif ($choice eq 'd') { delete_son_father_pair(); } elsif ($choice eq 'e') { $is_time_to_exit = 1; } elsif ($choice eq 'g') { get_a_father(); } elsif ($choice eq 'o') { output_hash_neatly(); } elsif ($choice eq 'r') { replace_a_father(); } elsif ($choice eq 'x') { get_a_grandfather(); } else { print "Invalid choice, TRY AGAIN!\n"; } } print "Come back again -- goodbye!"; #### sub add_son_father_pair { print "Enter a male name: "; chomp (my $name1 = lc ); if (exists $son_father{$name1}) { print "Duplicate name -- try again!\n"; return; } print "Add a father: "; chomp (my $add_dad = lc ); $son_father{$name1} = $add_dad; } #### # Nothing special about these: sub add_son_father_pair { die "NYI" } sub delete_son_father_pair { die "NYI" } sub get_a_father { die "NYI" } sub output_hash_neatly { die "NYI" } sub replace_a_father { die "NYI" } sub get_a_grandfather { die "NYI" } # I wanted a bit more detail about this one, though. sub add_daily_task { die "NYI"; my ($task_name, $day_of_week, $task) = @_; # Dies on error, otherwise returns $time_of_day the task is scheduled for } #### $ grep NYI my_perl_script.pl sub add_son_father_pair { die "NYI" } sub delete_son_father_pair { die "NYI" } sub get_a_father { die "NYI" } sub output_hash_neatly { die "NYI" } sub replace_a_father { die "NYI" } sub get_a_grandfather { die "NYI" } sub add_daily_task { die "NYI"; #### sub add_son_father_pair { my $name1 = ask_question("Enter a male name: "); if (exists $son_father{$name1}) { print "Duplicate name -- try again!\n"; return; } my $add_dad = ask_question("Add a father: "); $son_father{$name1} = $add_dad; } sub ask_question { my $question = shift; print $question; chomp(my $answer = lc ); return $answer; } #### my $name1 = lc ask_question("Enter a male name: "); #### sub ask_question { my $question = shift; print $question; chomp(my $answer = lc ); return $answer; } sub ask_for_name { my $question = shift; return lc ask_question($question); } #### sub add_son_father_pair { # show the "before" data print "add_son_father_pair before\n"; output_hash_neatly(); my $name1 = ask_question("Enter a male name: "); if (exists $son_father{$name1}) { print "Duplicate name -- try again!\n"; return; } my $add_dad = ask_question("Add a father: "); $son_father{$name1} = $add_dad; # Did we do it right? print "add_son_father_pair after:\n"; output_hash_neatly(); } #### $ perl my_perl_script.pl Undefined subroutine &main::choose_menu_item called at my_perl_script.pl line 7. #### $ grep TODO my_perl_script.pl # TODO pull these next few lines into a frobnicate() subroutine # TODO Can solve world hunger .. but this line is too short to contain the solution # TODO Try to remember the solution to above TODO!