Bangladeshi Softwares
Home - Download - Codes & Tutorials - Bangla IT Bibhag - Mobile - About Me

 


Creating a simple Perl console menu


Step-1:
In this small code snippet, you will learn creating a simple Perl console menu. Open your favorite text editor and type the code listed below:


#!/usr/bin/perl

##
# Author: Ahsanul Haque Shovon
# Email: ahsanul.haque.shovon@gmail.com / ahsanul_haque_shovon@yahoo.com
# Web: http://shovon.110mb.com
##

$input = "";

print "\nSyntax\t\tMeaning\n";
print "======\t\t=======\n";
print "b\t\tBeep once\n";
print "r\t\tA random number (0 - 999)\n";
print "h\t\tPrint this menu\n";
print "q\t\tQuit application\n\n";

while(1)
{
  print "Enter your choice: ";
  chomp($input = <STDIN>);

  if($input eq "q")
  {
    last;
  }

  elsif($input eq "b")
  {
    print "\a\n";
  }

  elsif($input eq "r")
  {
    printf("%d\n\n", rand() * 1000);
  }

  elsif($input eq "h")
  {
    print "\nSyntax\t\tMeaning\n";
    print "======\t\t=======\n";
    print "b\t\tBeep once\n";
    print "r\t\tA random number (0 - 999)\n";
    print "h\t\tPrint this menu\n";
    print "q\t\tQuit application\n\n";
  }

  else
  {
    print "Invalid syntax. Type 'h' to get help.\n\n";
  }
}


Step-2:
Now save the code as "perl_menu.pl" and execute it.


On Windows: You will need to install Perl v5.x on your machine and set the path as necessary. This can be achived by setting the PATH variable. Modern Perl installers are smart enough to add their PATH to the SYSTEM PATH VARIABLE. If your PATH is not set, you can open a command prompt and type something like:
PATH=%PATH%;C:\perl\bin

Change the location "C:\perl\bin", if you installed Perl somewhere else. Now CD (Change Directory) to the directory where you saved this script and type:
perl perl_menu.pl


On UNIX: You should already have Perl installed on your machine. Open a terminal window, CD (Change Directory) to the directory where you saved this script and type:
perl perl_menu.pl





Other Helpful Resources:
http://www.activestate.com
http://www.perl.org



 



©Ahsanul Haque Shovon
All rights reserved