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 6639095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:30:50+00:00 2026-05-25T23:30:50+00:00

Is there any way to clear the STDIN buffer in Perl? A part of

  • 0

Is there any way to clear the STDIN buffer in Perl? A part of my program has lengthy output (enough time for someone to enter a few characters) and after that output I ask for input, but if characters were entered during the output, they are “tacked on” to whatever is entered at the input part. Here is an example of my problem:

for(my $n = 0; $n < 70000; $n++){
   print $n . "\n";
}
chomp(my $input = <STDIN>);
print $input . "\n";

The output would include any characters entered during the output from that for loop. How could I either disable STDIN or flush the STDIN buffer (or any other way to not allow extra characters to be inserted into STDIN before calling it)?

  • 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-25T23:30:51+00:00Added an answer on May 25, 2026 at 11:30 pm

    It looks like you can accomplish this with the Term::ReadKey module:

    #!perl
    
    use strict;
    use warnings;
    use 5.010;
    
    use Term::ReadKey;
    
    say "I'm starting to sleep...";
    ReadMode 2;
    sleep(10);
    ReadMode 3;
    my $key;
    while( defined( $key = ReadKey(-1) ) ) {}
    ReadMode 0;
    say "Enter something:";
    chomp( my $input = <STDIN> );
    say "You entered '$input'";
    

    Here’s what happens:

    • ReadMode 2 means “put the input mode into regular mode but turn off echo”. This means that any keyboard banging that the user does while you’re in your computationally-expensive code won’t get echoed to the screen. It still gets entered into STDIN‘s buffer though, so…
    • ReadMode 3 turns STDIN into cbreak mode, meaning STDIN kind of gets flushed after every keypress. That’s why…
    • while(defined($key = ReadKey(-1))) {} happens. This is flushing out the characters that the user entered during the computationally-expensive code. Then…
    • ReadMode 0 resets STDIN, and you can read from STDIN as if the user hadn’t banged on the keyboard.

    When I run this code and bang on the keyboard during the sleep(10), then enter some other text after the prompt, it only prints out the text I typed after the prompt appeared.

    Strictly speaking the ReadMode 2 isn’t needed, but I put it there so the screen doesn’t get cluttered up with text when the user bangs on the keyboard.

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

Sidebar

Related Questions

Is there any way to clear the Console? (I.e. all the output of my
Is there any way to make a telnet app to clear the output client-side
Is there any way, in any language, to hook my program when a user
Is there any way possible to clear whats currently in the view instead of
Is there any way I can implement functionality which will clear all my data/files/shared_preferences/database
Is there any way to clear the F# interactive window in VS2010? #cls;; and
In php, is there any way to clear/remove all previously echoed or printed items?
How can I clear the cache in gwt ? or is there any way
Is there any way to reset/clear the browser's localStorage in javascript?
Is there any way to clear Browser History using C# or VB.net desktop application.

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.