Saturday, February 2, 2013

menu driven script for creating and storing the INDEX to a file

Following are the functions of the script:

1) show the index stored in file
2) search the index by keyword
3) add the record to index (key - value pair).

------------------------------------------------------------------------------------
menu_driven_create_store_index.pl

#!/usr/bin/perl -w
#use strict;
use Switch;
use Data::Dumper;

MENU:
system("clear");

showMenu();
my $choice = getChoice();

decide($choice);

askForExit();

#------------------------------SUBROUTINES----------------------------

sub showMenu {
  print "---------------------MENU-------------------\n";
  print "1)Show Index\n2)Search Index by keword(s)\n3)Add tutorial to the Index\n4)Exit\n";
  print "--------------------------------------------\n";
  print "\nPlease enter choice: ";
}


sub getChoice {
  my $choice = <>;
  chomp $choice;
  return $choice;
}


sub decide {
  my $choice = shift;
  switch ($choice) {
    case 1 {showIndex();}
    case 2 {
             my $keword = shift;
             searchByKeyword($keword);
           }
    case 3 {addTutorialToIndex();}
    else {print "Wrong choice\n";}
  }
}

sub showIndex {
  open FH, 'index' or die "cant open index : $!";
  local $/;
  my $hashref = eval <FH>;
  close FH;

  print "\n---------------INDEX-------------\n";
  foreach my $key (keys %$hashref) {
    print "> $key => ", $hashref->{$key}, "\n";
  }
}

sub addTutorialToIndex {

  open  FH1, 'index' or die "cant open index : $!";
  local $/;
  my $hashref = eval <FH1>;
  close FH1;

  $/ = "\n";

  $hashref = {} unless ($hashref);

  print "\nEnter the index key:\n";
  my $key = <>;
  chomp($key);

  print "\nEnter the index value i.e. filename in which the tutorial is stored\n";
  my $value = <>;
  chomp($value);

  if (exists $hashref->{$key}) {
    print "\n\nThe index key already exists.\nDo you want to override?(y/n)\n";
    my $choice = acceptYesNoChoice();
    return if ($choice eq 'n');
  }
  $hashref->{$key} = $value;

  open  FH, '>', 'index' or die "cant open index : $!";
  print FH Dumper($hashref);
  close FH;
  print "\n\n Data added to the Index.";
}

sub acceptYesNoChoice {
  my $choice;
  while (1) {
    $choice = <>;
    chomp $choice;
    return $1 if ($choice =~ /^(y|n)$/);
    print "\n\nInvalid Choice!!\nPlease enter valid choice (y/n)";
  }
}

sub askForExit {
 print "\n\nDo you wish to continue(y/n):\n";
  $choice = acceptYesNoChoice();
  goto MENU if ($choice eq 'y');
}

---------------------------------------------------------------

>perl menu_driven_create_store_index.pl

---------------------MENU-------------------
1)Show Index
2)Search Index by keword(s)
3)Add tutorial to the Index
4)Exit
--------------------------------------------

Please enter choice: 1

 choice = 1

---------------INDEX-------------
> calling subroutine => ex1.pl
> When you pass scalars to subroutine, they are passed by reference => ex2.pl
> Setting the default values for arguments => ex3.pl


Do you wish to continue?(y/n):



Likewise you can carryout other functions and you can add more functions according to you need.

No comments:

Post a Comment