[Sunhelp] Phonebook Program for UNIX?

Dennis L. Lund dllund at hermes.nextel.com
Mon Sep 25 22:47:18 CDT 2000


----------
X-Sun-Data-Type: text
X-Sun-Data-Description: text
X-Sun-Data-Name: text
X-Sun-Charset: us-ascii
X-Sun-Content-Lines: 10

I use the attached Perl program as my addressbook.  I got it from a magazine some time
ago.  Can't remember which one.

The program is addrmenu,  the menufile.txt is the menu layout for the program.
Touch contacts.txt file.  This is where your info is stored.

I either use vi to modify a record, or remove the record and re-enter it it
more than just the phone number changes.

Dennis Lund
----------
X-Sun-Data-Type: default-app
X-Sun-Data-Description: default
X-Sun-Data-Name: addrmenu
X-Sun-Content-Lines: 318
X-Sun-Charset: us-ascii

#!/usr/local/bin/perl


#-------------------------------------------
# MAIN ROUTINE
#-------------------------------------------
# Display a menu and get a selection.
get_menu_pick();

# as long as the E(x)it option is not chosen,
# execute the menu optin and then display
# the menu again and ask for another choice.

while ( $pick ne "q" )
{
	do_pick();
	get_menu_pick();
}

# Clear the screen and exit with a 0 return code.
clear_screen();

exit (0);
#------------------------------------------
# MAIN ROUTINE ENDS
#------------------------------------------

# Clear the screen, Show the menu and get user input.
sub get_menu_pick
{
	clear_screen();
	show_menu();
	get_pick();
}

# Clear the screen by printing 25 newlines.
sub clear_screen
{
	for ($i=0; $i < 25; ++$i) {
		print "\n";
	}
}

# Open menufile.txt or exit with an error
# read in each row picking up the first two fields by
# splitting it on the pipe |
# print the first two fields
# send some form feeds to do some centering.
sub show_menu
{
	$count = 0;
	open( MENUFILE, "menufile.txt") or die "Can't open menufile.txt $!\n";
	while ($menurow=<MENUFILE>)
	{
		($menupick,$menuprompt)=split /:/,$menurow;
		print "\t$menupick\t$menuprompt \n";
		++$count;
	}
	close MENUFILE;
	print "\tq\tExit\n";
	++$count;
	$count = (24 - $count ) /2;
	for ($i=0; $i < $count; ++$i){
		print "\n";
	}
	print "\n\nEnter your selection\n";

}

# Get user input and chop off the newline.
sub get_pick()
{
	chomp($pick = <STDIN>);
}

sub do_pick()
{

	open( MENUFILE, "menufile.txt") or die "Can't open menufile.txt: $!\n";
	while ($menurow=<MENUFILE>)
	{
		($menupick,$menuprompt,$menucommand,$menutype)=split /:/,$menurow;
		if ($menupick eq $pick)
		{
			if ($menutype eq "system")
				{
				system $menucommand;
				}
			else
				{
				&$menucommand;
				}
			break;
		}
	}
	close MENUFILE;
	press_enter();
}

#Put up a message and wait for user to press ENTER.
sub press_enter
{
	print "Press Enter to Contune . . .\n";
	$dummy = <STDIN>;
}

#------------------------------------------------------
# Add_contact() routine and supporing routines.
#------------------------------------------------------

# Get data for each of the fields i the contact file
# verify that the data is correct and write it to file.

sub add_contact
{
	$first = get_data (1, "First Name");
	$last = get_data (2, "Last Name");
	$company = get_data (3, "Company Name");
	$address1 = get_data (4, "Address 1");
	$address2 = get_data (5, "Address 2");
	$city = get_data (6, "City");
	$state = get_data (7, "State");
	$zip = get_data (8, "Zip");
	$phone = get_data (9, "Phone");
	$fax = get_data (10, "Fax");
	$email = get_data (11, "E-mail");

	is_it_ok();
	write_contact();
}

# prompt and enter data
sub get_data
{
	my ($num, $prompt) = @_;
	print "\t\t$num. Please enter $prompt?\n";
	chomp ( my $res = <STDIN> );
	return $res;
}

# show the user the entry and ask if its OK
# allow changes if not
sub is_it_ok
{
	$ans = "n";
	while ($ans eq "n")
	{
		print_contact();
		print "Is this correct? ";
		$ans = get_yes_no();
		if ($ans eq "n") {get_changes();}
	}
}

# print all fields of a contact
sub print_contact
{
	print_data (1, "First Name",$first);
	print_data (2, "Last Name",$last);
	print_data (3, "Company Name",$company);
	print_data (4, "Address 1",$address1);
	print_data (5, "Address 2",$address2);
	print_data (6, "City",$city);
	print_data (7, "State",$state);
	print_data (8, "Zip",$zip);
	print_data (9, "Phone",$phone);
	print_data (10, "Fax",$fax);
	print_data (11, "E-mail",$email);
}

# print one field of a contact
sub print_data
{
	my ($num, $prompt, $value) = @_;
	print "\t\t$num. \t$prompt \t$value\n";
}

# ask for a yes or no answer
sub get_yes_no
{
	print "yes/no (y/n)\n";
	chomp ( my $res = <STDIN> );
	return $res;
}

# get the number of the field to change and then ask the
# user for new data
sub get_changes
{
	print "Which field do you want to change (99 to exit)?\n";
	chomp ( my $num = <STDIN> );
	while ($num != 99)
	{
		change_field($num);
		print "Which field do you want to change (99 to exit)?\n";
		chomp ( $num = <STDIN> );
	}
}

# based on the number of the field to change
# ask the user for new data
sub change_field
{
	my ($nm) = @_;
	SWITCH:{
	if ($nm==1) {$first=get_data($nm, "First Name");last SWITCH;}
	if ($nm==2) {$last=get_data($nm, "Last Name");last SWITCH;}
	if ($nm==3) {$company=get_data($nm, "Company Name");last SWITCH;}
	if ($nm==4) {$address1=get_data($nm, "Address 1");last SWITCH;}
	if ($nm==5) {$address2=get_data($nm, "Address 2");last SWITCH;}
	if ($nm==6) {$city=get_data($nm, "City");last SWITCH;}
	if ($nm==7) {$state=get_data($nm, "State");last SWITCH;}
	if ($nm==8) {$zip=get_data($nm, "Zip");last SWITCH;}
	if ($nm==9) {$phone=get_data($nm, "Phone");last SWITCH;}
	if ($nm==10) {$fax=get_data($nm, "Fax");last SWITCH;}
	if ($nm==11) {$email=get_data($nm, "E-mail");last SWITCH;}
	}
}

# write all fields to contact.txt with : delimiters
sub write_contact
{
	open (CONTACTS,">>contact.txt");
	print CONTACTS "$first:$last:$company:$address1:$address2:$city:$state:$zip:$phone:$fax:$email\n";
	close CONTACTS;
}

#------------------------------------
# lookup_contact() routine and supporting routines
#------------------------------------

# Ask the user for a last name to look up and then search the
# contact.txt file for it
sub lookup_contact
{
	print "Enter the last name to look for\n";
	chomp ( my $lookup=<STDIN> );
	if (0 == lookup_this_contact($lookup))
		{
		print "$lookup not found\n";
		}
	else
		{
		print "Last entry has been displayed\n";
		}
}

# open the contact.txt file and read through it looking for
# a match on the passed last name field.  Display the contact
# data anytime the last name matches.
sub lookup_this_contact
{
	my $found = 0;
	my ($lu)=@_;

	open (CONTACTS, "contact.txt") or die "Can't open contact.txt: $!\n";
	while ($datarow=<CONTACTS>)
	{
		@data=split /:/,$datarow;
		($first,$last,$company,$address1,$address2,$city,$state,$zip,$phone,$fax,$email)=@data;
		if ($lu eq $last)
			{
			$found = 1;
			print_contact();
			press_enter();
			}
	}
	close CONTACTS;
	return $found
}

#------------------------------------
# print_contact() routine
#	using support routines from other functions
#------------------------------------

# step through the contact file listing contact imformation
sub print_contacts
{
	open (CONTACTS, "contact.txt") or die "Can't open contact.txt: $!\n";
	while ($datarow=<CONTACTS>)
	{
		@data=split /:/,$datarow;
		($first,$last,$company,$address1,$address2,$city,$state,$zip,$phone,$fax,$email)=@data;
		print_contact();
		press_enter();
		clear_screen();
	}
	close CONTACTS;
	print "Last Entry has been displayed.\n";
}

# Remove a contact from the contact.txt file
sub rem_contact
{
	print "Enter the last name to look for\n";
	chomp ( my $remname=<STDIN> );
	print "Enter the file name to look in\n";
	chomp ( my $filename=<STDIN> );
	my $command1 = "grep -v $remname $filename > $filename.new";
	my $command2 = "mv $filename.new $filename";

	open( CONTACTS, "contact.txt") or die "Can't open contact.txt: $!\n";
        while ($datarow=<CONTACTS>)
	{
		@data=split /:/,$datarow;
		($first,$last,$company,$address1,$address2,$city,$state,$zip,$phone,$fax,$email)=@data;
		if ($remname eq $last)
		{
			$found = 1;
			system $command1;
			system $command2;
		}
	}

	close CONTACTS;
	return $found
}
----------
X-Sun-Data-Type: default
X-Sun-Data-Description: default
X-Sun-Data-Name: menufile.txt
X-Sun-Content-Lines: 4
X-Sun-Charset: us-ascii

a:Add a New Contact:add_contact:perl
b:Display Contact Information:lookup_contact:perl
c:Display All Contacts:print_contacts:perl
d:Remove Contact:rem_contact:perl





More information about the SunHELP mailing list