Author: Not specified Language: perl
Description: Not specified Timestamp: 2017-09-23 18:25:40 +0000
View raw paste Reply
  1. my %hosts;
  2.  
  3. Menu();
  4.  
  5. sub Menu{
  6.     my $choice;
  7.     print "Choose an option:\n1-Add a host\n2-Search a host\n3-Delete a host\n4-Edit a host\n5-Show all hosts\n6-Exit\n";
  8.     $choice = <>;
  9.  
  10.     if($choice == 1){
  11.         Add();
  12.     }
  13.     elsif($choice == 2){
  14.         print "Enter the name of the host you want to search:\n";
  15.         my $sc = <>;
  16.  
  17.         Search($sc);
  18.     }
  19.     elsif($choice == 3){
  20.         print "Enter the name of the host you want to delete:\n";
  21.         my $dc = <>;
  22.  
  23.         Delete($dc);
  24.     }
  25.     elsif($choice == 4){
  26.         print "Enter the name of the host you want to edit:\n";
  27.         my $ec = <>;
  28.  
  29.         Edit($ec);
  30.     }
  31.     elsif($choice == 5){
  32.         ShowAll();
  33.     }
  34.     elsif($choice == 6){
  35.         Exit();
  36.     }
  37.     else{
  38.         print "There is no such option!";
  39.         Menu();
  40.     }
  41. }
  42.  
  43. sub Add{
  44.     print "Enter the Host's name: ";
  45.     my $name = <>;
  46.     print "Enter the Host's IP adress: ";
  47.     my $ip = <>;
  48.     $hosts{$name} = $ip;
  49.     print "Host $del successfully added!\n\n";
  50.  
  51.     Menu();
  52. }
  53.  
  54. sub Search{
  55.     my $srch = $_[0];
  56.  
  57.     if( exists($hosts{$srch} ) ){
  58.         print "$srch";
  59.         print "$hosts{$srch}\n";
  60.     }
  61.     else{
  62.         print "This Host does not exists!\n\n";
  63.     }
  64.  
  65.     Menu();
  66. }
  67.  
  68. sub Delete{
  69.     my $del = $_[0];
  70.  
  71.     if( exists($hosts{$del} ) ){
  72.         delete $hosts{$del};
  73.         print "Host $del successfully deleted!\n\n";
  74.     }
  75.     else{
  76.         print "This Host does not exists!\n\n";
  77.     }
  78.  
  79.     Menu();
  80. }
  81.  
  82. sub Edit{
  83.     my $edit = $_[0];
  84.  
  85.     if( exists($hosts{$edit} ) ){
  86.         print "Enter the Host's new name: \n";
  87.         my $editname = <>;
  88.         print "Enter the Host's new IP adress: \n";
  89.         my $editip = <>;
  90.         $hosts{$editname} = $editip;
  91.                
  92.         delete $hosts{$edit};
  93.         print "Host $edit successfully changed!\n\n";
  94.     }
  95.     else{
  96.         print "This Host does not exists!\n\n";
  97.     }
  98.  
  99.     Menu();
  100. }
  101.  
  102. sub ShowAll{
  103.     my @names = keys %hosts;
  104.     my @ip = values %hosts;
  105.     my $size = @names;
  106.     my $aux;
  107.  
  108.     for( $aux = 0; $aux < $size; $aux = $aux + 1 ){
  109.         print "$names[$aux]";
  110.         print "$ip[$aux]";
  111.     }
  112.     print "\n";
  113.        
  114.     Menu();
  115. }
  116.  
  117. sub Exit{
  118.     exit(0);
  119. }
View raw paste Reply