代码之家  ›  专栏  ›  技术社区  ›  sid_com

可以用Term::ReadKey清除终端吗?

  •  -1
  • sid_com  · 技术社区  · 14 年前

    有没有办法对Term::ReadKey模块这么做?

    #!/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();
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   Sinan Ünür    14 年前

    我不知道为什么 Term::ReadKey 会提供这样的功能,或者如果它提供了。但是,怎么样:

    #!/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();
    
        2
  •  1
  •   David W.    14 年前

    不确定为什么要使用 Term::Readkey 用于清除屏幕。它绝对没有这种能力。您是否在尝试使用标准Perl安装的一部分?您可以使用Term::Caps,它是标准Perl安装的一部分。不幸的是,它需要Termcaps文件在系统上,而Windows没有这个文件。

    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";
    

    如果是Windows终端,我试着打印ANSI转义序列,但似乎不起作用。

    我讨厌打系统电话,因为有安全隐患。如果有人改变了 cls 命令你?

    推荐文章