【问题标题】:What's the best way to clear the screen in Perl?在 Perl 中清除屏幕的最佳方法是什么?
【发布时间】:2008-10-13 15:13:10
【问题描述】:

理想情况下,跨平台的东西。

【问题讨论】:

  • 控制台屏幕还是 GUI 屏幕?

标签: perl


【解决方案1】:
print "\033[2J";    #clear the screen
print "\033[0;0H"; #jump to 0,0

【讨论】:

  • +1000 给这个人,这种方法不需要用户安装一些 cpan 模块(以及其他所有内容)
  • 不需要安装,也不能在某些平台上运行。
  • 很酷,但是在 Windows 上使用什么?
【解决方案2】:

CPAN 可能是最好的选择。看看Term::Screen:Uni

require Term::Screen::Uni;
my $scr = new Term::Screen::Uni;

$scr->clrscr()

【讨论】:

    【解决方案3】:

    我通常使用来自 CPAN 的 Term::ANSIScreen,它为我提供了各种与控制台相关的有用功能。

    use Term::ANSIScreen qw(cls);
    cls();
    

    【讨论】:

      【解决方案4】:

      来自perlfaq8How do I clear the screen的回复:


      要清除屏幕,您只需打印告诉终端清除屏幕的特殊序列。一旦你有了那个序列,当你想清屏时输出它。

      您可以使用 Term::ANSIScreen 模块来获取特殊序列。导入 cls 函数(或 :screen 标签):

      use Term::ANSIScreen qw(cls);
      my $clear_screen = cls();
      
      print $clear_screen;
      

      如果要处理终端控制的低级细节,Term::Cap 模块也可以获取特殊序列。 Tputs 方法返回给定功能的字符串:

      use Term::Cap;
      
      $terminal = Term::Cap->Tgetent( { OSPEED => 9600 } );
      $clear_string = $terminal->Tputs('cl');
      
      print $clear_screen;
      

      在 Windows 上,您可以使用 Win32::Console 模块。在为要影响的输出文件句柄创建对象后,调用 Cls 方法:

      use Win32::Console;
      
      $OUT = Win32::Console->new(STD_OUTPUT_HANDLE);
      $OUT->Cls;
      

      如果你有一个命令行程序来完成这项工作,你可以在反引号中调用它来捕获它输出的任何内容,以便以后使用它:

      $clear_string = `clear`;
      
      print $clear_string;
      

      【讨论】:

      • Win32::Console::Cls 没有返回可以保存的值。
      • 你会如何改变 perlfaq 中的答案来解决这个问题?
      【解决方案5】:

      在 OS X 和 Linux 下,可以使用以下 Perl 命令:

      system("clear");
      

      不知道Windows下的等价物是什么。

      编辑:Windows 等效项是:

      system("cls");
      

      【讨论】:

        【解决方案6】:

        如果您在谈论终端,我会使用 Curses lib 之类的东西来完成它。

        有一个不错的 Curses 模块可以访问它,您可以像这样使用它:

        perl -MCurses -e '$win=new Curses;$win->clear()'
        

        【讨论】:

          【解决方案7】:

          我不同意上述观点

          1. 连接其他模块 = 增加攻击面。
          2. 减少运行代码量。
          3. 代码重构。

          #!/usr/bin/perl -w
          use strict;
          
          my
          ( $over, $cleaning );
          ( $cleaning ) = qq([J\033[H\033[J);
          ( $over ) = <<EOF;
           1. Connecting additional modules = Increase the attack surface.
           2. Reduce the Amount of Running Code.
           3. Code refactoring.
          EOF
          
          print ($cleaning.$over);
          
          __END__
          
          FOO BAR
          \033 stands for ESC ANSI value 27
          [J erases the screen from the current line down screen
          [H move the cursor to the home position

          【讨论】:

          • 很好的答案,尽管我会从您的答案中的实际代码中删除不必要的 $over 部分。它们是仅将$cleaning 打印到屏幕上的良好动机,但不是真正解决方案的一部分。
          • Сharm Perl 是 - «有不止一种方法可以做到这一点» ;)
          【解决方案8】:

          支持 Linux 和 Windows:

          system($^O eq 'MSWin32'?'cls':'clear');
          

          【讨论】:

            猜你喜欢
            • 2021-06-06
            • 1970-01-01
            • 1970-01-01
            • 2010-09-15
            • 1970-01-01
            • 1970-01-01
            • 2010-10-17
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多