#!/usr/bin/perl -w use strict; print " Welcome to your own Personal Address Book!\n"; my $explain_text = " Command are: add Add An Entry del Delete An Entry prn Print all Entries end End Program quit Also Ends program "; my $short_prompt = "add,del,prn or quit: "; my $input; my %directory=(); print $explain_text; #don't keep printing this long stuff while ( ($input=prompt_for_input($short_prompt)), $input !~ /\s*end|quit\s*$/i) { next if $input =~ /^\s*$/; # re-prompt on blank lines # this is not an error! if ($input =~ /^\s*add\s*$/) { add_entry(); } elsif ($input =~ /^\s*del\s*$/) { delete_entry(); } elsif ($input =~ /^\s*prn\s*$/) { print_directory(); } else { print "illegal command!\n"; } } sub prompt_for_input { my $message = shift; print $message; my $input = <>; $input =~ s/^\s*//; # no leading spaces $input =~ s/\s*$//; # no trailing spaces return $input; } sub add_entry { my $name = prompt_for_input ("Enter the Name you would like to add: "); my $address = prompt_for_input ("Enter an Address for $name: "); $directory{$name} = $address; } sub delete_entry { print "some code to get name to delete goes here\n"; } sub print_directory { print "These are the people in your address book:\n"; printf "%-15s %s\n", "Name", "Address"; foreach my $who (sort keys %directory) { printf "%-15s %s\n", $who, $directory{$who}; } print "\n"; }