Madhan
Madhan

Reputation: 1321

Clear already printed values using perl

I need to clear the printed values in perl console window. For an example, Note: I am developing this in Windows OS.

use strict;

my $mode;

Initialize();

sub Initialize{
    print "Enter 1 or 2";
    $mode=<STDIN>;
    chomp($mode); 
    check_mode($mode);
}

sub check_mode{
    if(($mode!=1) and ($mode!=2)){
        print "invalid selection";
        Initialize();
    }
    else{
        print "valid selection";
        sleep 5;
   }
}

While entering wrong selection, I have called the Initialize function, it is printing again. But, what I want is while calling the function it should delete already printed value in the console window and it should print again. Is it possible?

Please give your valuable suggestions.

Upvotes: 1

Views: 204

Answers (2)

run
run

Reputation: 1186

for specific to window os and linux os

system($^O =~ /win/i ? 'cls' : 'clear');

Upvotes: 1

David Wheaton
David Wheaton

Reputation: 618

While you can use the backspace character code "\b" to erase characters on the current line, that has limitations since when the user hits enter it will print a linefeed and your backspace characters won't carry back up to erase the previous line.

See Win32::Console which should allow you to print your prompt at a fixed location and then later overwrite the wrong selection or you can get input a single character at a time using the InputChar method and suppress the newline...

Upvotes: 2

Related Questions