Advertisement
jrp370

Phonebook

Mar 7th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6.54 KB | None | 0 0
  1. #!/usr/bin/perl -wT
  2.  
  3. use strict;
  4.  
  5. use lib "/usr/local/custom/lib"; # A directory containing Perl modules.
  6.  
  7. use Utilities; # A particular module to use. (Utilities.pm)
  8.  
  9. use DBI;
  10.  
  11. use Term::ANSIColor qw(:constants);
  12.  
  13. # James Park
  14.  
  15. # Created on 2-01-11
  16.  
  17. # This creates a phonebook and stores it all in a database table
  18.  
  19.  
  20.  
  21.  
  22.  
  23. #############################################################
  24.  
  25.  
  26.  
  27. my $sth; # statement handle
  28.  
  29. my $name;
  30.  
  31. my $PhoneNumber;
  32.  
  33.  
  34.  
  35. #############################################################
  36.  
  37.  
  38.  
  39. # Get database username from unix username and database password from STDIN.
  40.  
  41. my $username = Utilities::username(); # Find unix username of the current user.
  42.  
  43. my $dbusername = $username;
  44.  
  45. my $dbname = $username;
  46.  
  47.  
  48.  
  49. print "Enter your database password: ";
  50.  
  51. use Term::ReadKey;
  52.  
  53. ReadMode 'noecho';
  54.  
  55. my $dbpassword = ReadLine(0);
  56.  
  57. print "\n";
  58.  
  59. ReadMode 'normal';
  60.  
  61. chomp $dbpassword;
  62.  
  63.  
  64.  
  65. #############################################################
  66.  
  67.  
  68.  
  69. # Open database and create database handle
  70.  
  71. my $dbh =
  72.  
  73.   DBI->connect("DBI:mysql:$dbusername;host=localhost;port=3306",
  74.  
  75.                "$dbname",
  76.  
  77.                "$dbpassword")
  78.  
  79.     or die "Can't connect: " . DBI->errstr();
  80.  
  81.  
  82.  
  83. # For postgress use: Pg instead of mysql, port 5432.
  84.  
  85.  
  86.  
  87. #######################
  88.  
  89.  
  90.  
  91.  
  92.  
  93. my %phonebook;
  94.  
  95. while (1) {
  96.  
  97.     my $choice = menu();
  98.  
  99.     if ($choice == 1) {
  100.  
  101.         addEntry();
  102.  
  103.     } elsif ($choice == 2) {
  104.  
  105.          delete_entry();
  106.  
  107.     } elsif ($choice == 3) {
  108.  
  109.         searchEntry();
  110.  
  111.     } elsif ($choice == 4) {
  112.  
  113.         showEntry();
  114.  
  115.     }elsif ($choice == 5) {
  116.  
  117.         clearTable();
  118.  
  119.     } elsif ($choice == 6 ) {
  120.  
  121.         exit();
  122.  
  123.     } else {
  124.  
  125.         print "Not a choice, please try again";
  126.  
  127.     }
  128.  
  129. }
  130.  
  131.  
  132.  
  133. ###############################################
  134.  
  135.  
  136.  
  137. sub menu {
  138.  
  139.     print RED "1. Add an entry to your phonebook.\n";
  140.  
  141.     print  "2. Delete someone from your phonebook.\n";
  142.  
  143.     print  "3. Look up someones number.\n";
  144.  
  145.     print  "4. List all entries.\n";
  146.  
  147.     print  "5. Delete entire phonebook and start over.\n";
  148.  
  149.     print  "6. Close phonebook.\n";
  150.  
  151.     print  "Enter your choice (1-6): ";
  152.  
  153.     my $prompt = <STDIN>;
  154.  
  155.     return $prompt;
  156.  
  157. }
  158.  
  159.  
  160.  
  161. ################################################
  162.  
  163.  
  164.  
  165. sub addEntry {
  166.  
  167.    
  168.  
  169.     print BLACK "Please enter their name:";
  170.  
  171.     chomp (my $name = <STDIN>);
  172.  
  173.     $name = lc($name);
  174.  
  175.     print"Please enter their phone number (123-4567):";
  176.  
  177.     chomp (my $phone = <STDIN>);
  178.  
  179.     if ($phone !~ m|^\d\d\d\D\d\d\d\d$|) {
  180.  
  181.     print "'$phone' is not in correct '123 4567' format.\n";
  182.  
  183.     return ;}  
  184.  
  185.     my $sth; # statement handle
  186.  
  187.     $sth = $dbh->prepare('SELECT name FROM Phonebook WHERE name = ?');
  188.  
  189.     $sth->execute($name);
  190.  
  191.         my $ref=$sth->fetchrow_hashref();
  192.  
  193.     if (defined $ref->{'name'}){
  194.  
  195.         print "$name is already in the phonebook.\n";
  196.  
  197.        
  198.  
  199.         }
  200.  
  201.     else{
  202.  
  203.         $sth = $dbh->prepare('INSERT INTO Phonebook (name, Phone)           VALUES (?, ?)')
  204.  
  205.                 or die "Couldn't prepare statement: " . $dbh->errstr;
  206.  
  207.                
  208.  
  209.             $sth->execute($name, $phone);
  210.  
  211.             print "name: $name at $phone have been added.\n";
  212.  
  213.        
  214.  
  215.        
  216.  
  217.     }
  218.  
  219. }
  220.  
  221.  
  222.  
  223. ###############################################
  224.  
  225. sub delete_entry {
  226.  
  227.   print BLACK"If youre sure you want to DELETE this person enter their name> ";
  228.  
  229.  
  230.  
  231.   my $dbh =DBI->connect("DBI:mysql:jpark005;host=localhost;port=3306",
  232.  
  233.                "$dbname",
  234.  
  235.                "$dbpassword")
  236.  
  237.    
  238.  
  239.                 or die "Couldn't connect to database: " . DBI->errstr;
  240.  
  241.         my $sth = $dbh->prepare('DELETE FROM Phonebook WHERE name = ?')
  242.  
  243.                 or die "Couldn't prepare statement: " . $dbh->errstr;
  244.  
  245.   chomp (my $delname = <STDIN>);
  246.  
  247.   $delname = lc($delname);
  248.  
  249.   $sth->execute($delname)             # Execute the query
  250.  
  251.             or die "Couldn't execute statement: " . $sth->errstr;
  252.  
  253.   print "$delname was removed from your phonebook\n";
  254.  
  255.   $dbh->disconnect;
  256.  
  257.   $sth->finish;
  258.  
  259.  
  260.  
  261. }
  262.  
  263.  
  264.  
  265. ##################################################
  266.  
  267.  
  268.  
  269.  
  270.  
  271. sub searchEntry {
  272.  
  273.    
  274.  
  275.     my $dbh =DBI->connect("DBI:mysql:jpark005;host=localhost;port=3306",
  276.  
  277.                "$dbname",
  278.  
  279.                "$dbpassword")
  280.  
  281.    
  282.  
  283.                 or die "Couldn't connect to database: " . DBI->errstr;
  284.  
  285.         my $sth = $dbh->prepare('SELECT * FROM Phonebook WHERE name = ?')
  286.  
  287.                 or die "Couldn't prepare statement: " . $dbh->errstr;
  288.  
  289.  
  290.  
  291.         print BLACK"Enter the name you would like to search for, hit<enter> to quit> ";
  292.  
  293.        
  294.  
  295.         while (my $name = <>) {               # Read input from the user
  296.  
  297.           my @data;
  298.  
  299.           chomp $name;
  300.  
  301.           $sth->execute($name)             # Execute the query
  302.  
  303.             or die "Couldn't execute statement: " . $sth->errstr;
  304.  
  305.  
  306.  
  307.           # Read the matching records and print them out          
  308.  
  309.           while (@data = $sth->fetchrow_array()) {
  310.  
  311.             my $phone = $data[1];
  312.  
  313.            
  314.  
  315.             print "\t  $name:$phone  \n";
  316.  
  317.           }
  318.  
  319.  
  320.  
  321.           if ($sth->rows == 0) {
  322.  
  323.             print "No matching names were found.\n\n";
  324.  
  325.             $dbh->disconnect;
  326.  
  327.            
  328.  
  329.            
  330.  
  331.             return;
  332.  
  333.            
  334.  
  335.           }
  336.  
  337.           $sth->finish;
  338.  
  339.           print "\n";
  340.  
  341.           print "Enter name> ";
  342.  
  343.          
  344.  
  345.            
  346.  
  347.         }
  348.  
  349.          
  350.  
  351.  }            
  352.  
  353.    
  354.  
  355.  
  356.  
  357.  
  358.  
  359. ###################################################
  360.  
  361. sub showEntry{
  362.  
  363.     my $dbh =DBI->connect("DBI:mysql:jpark005;host=localhost;port=3306",
  364.  
  365.                "$dbname",
  366.  
  367.                "$dbpassword")
  368.  
  369.                 or die "Couldn't connect to database: " . DBI->errstr;
  370.  
  371.        
  372.  
  373.         my $sth = $dbh->prepare('SELECT * FROM Phonebook')
  374.  
  375.                 or die "Couldn't prepare statement: " . $dbh->errstr;
  376.  
  377.          
  378.  
  379.          $sth->execute()
  380.  
  381.             or die "Couldn't execute statement: " . $sth->errstr;
  382.  
  383.          my @data;
  384.  
  385.          while (@data = $sth->fetchrow_array()) {
  386.  
  387.         print BLACK "@data\n";
  388.  
  389.           }
  390.  
  391.           $sth->finish;
  392.  
  393.           $dbh->disconnect;
  394.  
  395.          
  396.  
  397.          
  398.  
  399.            
  400.  
  401.            
  402.  
  403.     }
  404.  
  405.  
  406.  
  407. ############################################
  408.  
  409. sub clearTable{
  410.  
  411.     # Clear table "Phonebook"
  412.  
  413.     $sth = $dbh->prepare('DELETE FROM Phonebook')
  414.  
  415.              or die "Can't prepare SQL: " . $dbh->errstr();
  416.  
  417.     $sth->execute()
  418.  
  419.             or die "Can't execute SQL: " . $sth->errstr();
  420.  
  421.     $sth->finish();
  422.  
  423.     print BLACK "The Phonebook was cleared\n\n";
  424.  
  425. }
  426.  
  427.  
  428.  
  429. ################################################## io::prompt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement