开发者

Is it possible to clear the terminal with Term::ReadKey?

开发者 https://www.devze.com 2023-01-26 02:25 出处:网络
Is th开发者_StackOverflowere a way to do this with the Term::ReadKey-module? #!/usr/bin/env perl use warnings;

Is th开发者_StackOverflowere a way to do this with the Term::ReadKey-module?

#!/usr/bin/env perl
use warnings;
use 5.012;
use Term::Screen;

say( "Hello!\n" x 5 );
sleep 2;

my $scr = Term::Screen->new();
$scr->clrscr();


I don't know why Term::ReadKey would provide such a feature or if it does. But, how about:

#!/usr/bin/env perl
use strict; use warnings;

*clrscr = $^O eq 'MSWin32'
        ? sub { system('cls') }
        : sub { system('clear') };

print "Hello\n" for 1 .. 5;
sleep 2;
clrscr();


Not sure why you want to use Term::Readkey for clearing the screen. It definitely does not have that capability. Are you trying to use something that's part of the standard Perl installation? You can use Term::Caps which is part of the standard Perl installation. Unfortunately, it requires the Termcaps file to be on the system, and Windows doesn't have that.

use Term::Cap;

#
# Use eval to catch the error when TERM isn't defined or their is no
# Termcap file on the system.
#
my $terminal;
eval {$terminal = Term::Cap->Tgetent();};

#
# Use 'cl' to get the Screen Clearing sequence
#

if ($@) {  #Most likely a Windows Terminal
    system('cls');            #We really should be doing the 2 line below
    # my $clear = "\e[2J";    #But, it doesn't seem to work.
    # print "$clear";         #Curse You! I'll get you yet Bill Gates!
} else {   #A Real Computer
    my $clear = $terminal->Tputs('cl');
    print "$clear";
}
print "All nice and squeeky clean!\n";

I tried printing the ANSI Escape sequence if it was a Windows Terminal, but it doesn't seem to work.

I hate doing system calls because there is a security risk . What if someone changed the cls command on you?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号