#!/usr/bin/perl -W
# Written by sulfericacid with much help from the monks from this wond
+erful site.
# Modifying an entry isn't enabled on this version, that seems too com
+plex for my abilities right now but if
# you have any pointers on how to accomplish it please let me know!
# I had to use SDBM_File because oddly enough I didn't have DB_File, c
+hange this accordingly.
use strict;
use warnings;
use POSIX;
use Fcntl;
use SDBM_File;
my %dbm;
my $test = "phone.dbm";
my $name;
tie (%dbm, 'SDBM_File', $test, O_CREAT|O_RDWR, 0644)
|| die "Died tying database\nReason: $!";
# Printing option menu
print "Aaron's Phonebook System\n\n\n";
print "f - find an entry\n";
print "a - add an entry\n";
# print "m - modify an entry\n";
print "d - delete an entry\n";
print "r - read all entries\n";
print "e - erase all entries\n";
chomp(my $option = <STDIN>);
$option =~s/\r//;
print "Option: $option\n";
#Add An Entry
if ($option eq 'a') {
print "add an entry test!\n";
print "Name:";
chomp(my $name = <STDIN>);
print "Phone number:";
chomp(my $number = <STDIN>);
print "Email Address:";
chomp(my $email = <STDIN>);
print "Address Line 1:";
chomp(my $address1 = <STDIN>);
print "Address Line 2:";
chomp(my $address2 = <STDIN>);
print "$name added to the list!\n";
# combine all data into one nicely snug package...I hope
$dbm{$name}=join "\0",$name,$number,$email,$address1,$address2;
$dbm{$name} =~s/\cM//g;
}
elsif ($option eq 'r') {
foreach my $key ( sort keys %dbm ) {
my($name, $number, $email, $addr1, $addr2) = split "\0", $dbm{$key
+};
print "$name\n$number\n$email\n$addr1\n$addr2\n\n\n";
}
}
elsif ($option eq 'f') {
print "Who are you looking for?\n";
&find;
}
# Delete An Entry
elsif ($option eq 'd') {
print "What name would you like removed?\n";
&del;
}
elsif ($option eq 'e') {
print "To delete all entries press y, to cancel press n\n";
&erase;
}
else {
print "Wrong button!\n";
}
sub del {
chomp(my $d = <STDIN>);
if ($dbm{$d}) {
delete $dbm{$d};
print "$d deleted successfully\n";
}
else {
print "$d not found\n";
}
}
sub find {
chomp(my $f = <STDIN>);
# $f=~tr/\r\z//d;
#$f =~s/\r\z//;
$f =~s/\cM//g;
if (exists $dbm{$f}) {
my($name, $number, $email, $addr1, $addr2) = split "\0", $dbm{$f}
+;
print "$name\n$number\n$email\n$addr1\n$addr2\n\n\n";
}
else {
print "Name $f cannot be found in the directory.\n";
}
}
sub erase {
chomp(my $e = <STDIN>);
$e =~s/\r//;
if ($e eq 'y') {
%dbm = ();
print "All entries have been deleted.\n";
}
elsif ($e eq 'n') {
print "Deleting process cancelled.\n";
}
else {
print "Key was invalid, deletion process failed\n";
}
}
untie %dbm;
|