#!/usr/bin/perl -W use strict; use warnings; use POSIX; use Fcntl; use CGI; use SDBM_File; my %dbm; my $test = "test.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;
update (broquaint): added <readmore> tag
In reply to Flawless Script problems? by sulfericacid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |