Prince Mavi wrote:
Hi folks
Hello,
I am new to perl. this is my first real program in perl.
The program is not doing what i intend it to do.
The problem in nut shell is that:
I expect to see the menu first and then input my choice.
but
the program asks for my choice first and then displays the menu
Please have a look and see what am i doing wrong.
Thanks a ton for your time guys.
code
----------------------------------------------------------
#!/usr/bin/perl
use strict;
use warnings;
sub dispBlanks {
print "\n\n";
}
sub clrScr {
system("clear");
}
sub dispMenu {
clrScr;
print "\n\n=== Student Manage Pro ===\n";
print "1. Display all students\n";
print "2. Display a particular student\n";
print "3. Add a new student\n";
print "4. Delete a student\n";
print "0. Exit\n";
print "Please choose one of the options\n";
}
while (1) {
dispMenu
my $choice = <STDIN>;
Your problem is here. You are calling the subroutine 'dispMenu' with
the argument 'my $choice = <STDIN>'. In order to pass the value of the
expression 'my $choice = <STDIN>' to the subroutine 'dispMenu' the
expression must be evaluated first so you must provide an option before
the menu is displayed.
chomp($choice); # to remove newline
print "you chose $choice";
Once that is fixed then this line will only diplay for a tiny fraction
of a second as you loop around and clear the screen right away.
dispBlanks;
}
----------------------------------------------------------
I would do it using strings instead of subroutines:
# #!/usr/bin/perl
use strict;
use warnings;
my $dispBlanks = "\n\n";
my $clrScr = `clear`;
my $dispMenu = <<MENU;
$clrScr
=== Student Manage Pro ===
1. Display all students
2. Display a particular student
3. Add a new student
4. Delete a student
0. Exit
Please choose one of the options
MENU
while ( 1 ) {
print $dispMenu;
my $choice = <STDIN>;
chomp $choice; # to remove newline
last if $choice == 0;
print "you chose $choice", $dispBlanks;
}
__END__
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/