Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6789353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:35:38+00:00 2026-05-26T17:35:38+00:00

For some reason, in my simple menu program, weird things happen as a result

  • 0

For some reason, in my simple menu program, weird things happen as a result of ReadKey()

#!/usr/local/bin/perl

use strict ;
use warnings ;
use English ;
use Term::ReadKey ;

my @available_choices = ('choice one', 'choice two', 'choice three') ;
my $array_size = scalar (@available_choices) ;

print "\nPlease make your selection from the options below:\n\n" ;

for (my $i=0, my $j=1 ; $i < $array_size ; $i++, $j++) {
  print "$j) $available_choices[$i]\n" ;
}

my $key = undef ;
for (my $k=0; $k < 5; $k++) {
  print "\nSelection :> " ;
  $key = ReadKey();
  if ((defined $key) && ($key =~ /[1-$array_size]/)) {
    print "\nYou selected \"$available_choices[$key-1]\"\n" ;
    last ;
  }
  else {
    sleep 1 ;
  }
}

So if you run this simple program and give 1, 2, or 3 as your selection it works as expected. If you enter anything else (to trigger the else block) the loop iterates like 3 or 4 times before ReadKey() accepts input, again. Best illustrated by this output (I entered xxx and then “Selection:>” printed 3 times before I was able to type yyy):

$ ./bar.pl 

Please make your selection from the options below:

1) choice one
2) choice two
3) choice three

Selection :> xxx

Selection :> 
Selection :> 
Selection :> 
Selection :> yyy
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T17:35:39+00:00Added an answer on May 26, 2026 at 5:35 pm

    It’s because ReadKey reads a key. When you press x 3 times and then Enter, that’s 4 keys. In fact, even a correct selection (1 Enter) is 2 keys; you just don’t notice because your program exits immediately.

    This is less obvious because the default input mode buffers keystrokes until you press Enter. At that point, ReadKey will start to return each keystroke one at a time.

    The solution depends on the behavior you’re looking for. If you want to have to press Enter before acting on the input, then you can just read a line at a time (with the standard <> operator). You don’t need Term::ReadKey at all.

    If you want to take action immediately after a keypress, you’ll need to use Term::ReadKey‘s ReadMode function to change the input buffering. Don’t forget to add END { ReadMode(0) } to restore the original mode when your program exits.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently got started trying to use SFML. For some reason my simple program
I recently got started trying to use SFML. For some reason my simple program
This should be pretty simple but for some reason, I can't get this program
Have a simple form (only extract fields here) but for some reason the JQserilization
Simple question, but for some reason I couldn't find the exact answer on Google:
I am trying something very simple, but for some reason it does not work.
This should be a really really simple thing, but for some reason it is
I think there is a simple answer to this, but for some reason I
I'm trying to do a very simple INSERT using VB.NET. For some reason I'm
I'm new to linq and having trouble writing two simple queries. For some reason,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.