I'm currently trying to learn perl as well as perl curses. I found this script, and I understand why it's supposed to work, but I don't understand why it doesn't.
The idea is that pressing "enter" selects one of the menu entries, and if any other key is pressed, is prints "Character pressed is $c". The numbers work as expected, but if you press a letter, it acts like you've pressed enter. - #!/usr/bin/perl
-
#
-
# Copyright (C) 2003 by Virtusa Corporation
-
# http://www.virtusa.com
-
#
-
# Anuradha Ratnaweera
-
# http://www.linux.lk/~anuradha/
-
#
-
-
-
use Curses;
-
-
$width = 30;
-
$height = 10;
-
$startx = 0;
-
$starty = 0;
-
-
@choices = (
-
"Choice 1",
-
"Choice 2",
-
"Choice 3",
-
"Choice 4",
-
"Exit"
-
);
-
-
$n_choices = @choices;
-
-
$highlight = 1;
-
$choice = 0;
-
-
initscr();
-
clear();
-
noecho();
-
cbreak();
-
$startx = ($COLS - $width) / 2;
-
$starty = ($LINES - $height) / 2;
-
-
$menu_win = newwin($height, $width, $starty, $startx);
-
keypad(1);
-
keypad($menu_win, 1);
-
addstr(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
-
refresh();
-
print_menu($menu_win, $highlight);
-
-
while (1) {
-
$c = getch($menu_win);
-
if ($c == KEY_UP) {
-
if ($highlight == 1) {
-
$highlight = $n_choices;
-
}
-
else {
-
$highlight--;
-
}
-
}
-
elsif ($c == KEY_DOWN) {
-
if ($highlight == $n_choices) {
-
$highlight = 1;
-
}
-
else {
-
$highlight++;
-
}
-
}
-
elsif ($c == '\n') {
-
$choice = $highlight;
-
}
-
else {
-
addstr($LINES - 2, 0, "Character pressed is $c");
-
refresh();
-
}
-
print_menu($menu_win, $highlight);
-
last if ($choice);
-
}
-
-
addstr($LINES - 2, 0, "You chose choice $choice with choice string $choices[$choice-1]");
-
getch();
-
clrtoeol();
-
refresh();
-
endwin();
-
-
sub print_menu {
-
$menu_win = shift;
-
$highlight = shift;
-
-
$x = 2;
-
$y = 2;
-
box($menu_win, 0, 0);
-
for ($i = 0; $i < $n_choices; $i++) {
-
if ($highlight == $i + 1) {
-
attron($menu_win, A_REVERSE);
-
addstr($menu_win, $y, $x, $choices[$i]);
-
attroff($menu_win, A_REVERSE);
-
}
-
else {
-
addstr($menu_win, $y, $x, $choices[$i]);
-
}
-
$y++;
-
}
-
refresh($menu_win);
-
}
-
1 1826
I know it's a little late, but if anyone else has this problem I figured I might as well post.
Line 63 is incorrect. This code was ported from C, where == works for everything, but perl likes to do things a little differently.
This:
should be: Sign in to post your reply or Sign up for a free account.
Similar topics |
by: windandwaves |
last post by:
Hi Folk,
Below is a message that I posted on alt.html I now have some more feedback
from people who experienced an error with safari and they tell me that the
PHPsessID changes while running a...
|
by: glenn |
last post by:
I have written a COM Server in C# and its working perfectly. I am not ready
to deploy to client computers to begin the real testing and am now wondering
how to perform. The part that confuses me...
|
by: noone |
last post by:
consider the following problem:
You have a C style library and API that uses callbacks to implement
functionality. Examples of this are X11 API, OpenGL/GLUT...The List goes
on.
The power of...
|
by: teddarr |
last post by:
I'm having trouble getting a mathmatical formula to work in my code.
I am supposed to write a program in java that calculates the ending balance each month. The user is supposed to input the...
|
by: shivkumar2004 |
last post by:
Hi!,
I am developing a chat system using vb.net in vs 2005. I am getting
the following error while registering the events.
error details: System.InvalidOperationException was unhandled...
| |
by: shivkumar2004 |
last post by:
Hi,
I m getting the following error while registering the events on client
appl.
error:
"An error occurred creating the form. See Exception.InnerException for
details. The error is: Exception...
|
by: Villanmac |
last post by:
First up thanks to the guys who pointed out my error last week.
Now I was wondering If I could get some help with this problem because again I have fallen into trouble. Basically im using the...
|
by: jthep |
last post by:
Hi,
I'm trying to get user input for a record book but I'm having trouble as I think I'm not making the getline function read the buffer correctly. I have the following variables declared in...
|
by: linuxfedora |
last post by:
I have written a C# program which has an activeX control (abc.ocx) in
Visual Studio 2005, and used the setup and deployment project to
create an setup.exe and setup.msi that act as an installer.
...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |