简体   繁体   English

在 Perl 中清除屏幕的最佳方法是什么?

[英]What's the best way to clear the screen in Perl?

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

print "\033[2J";    #clear the screen
print "\033[0;0H"; #jump to 0,0

The CPAN is probably the best way to go. CPAN 可能是最好的方法。 Take a look at Term::Screen:Uni :看看Term::Screen:Uni

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

$scr->clrscr()

I generally use Term::ANSIScreen from CPAN which gives me all sorts of useful console-related features.我通常使用来自 CPAN 的Term::ANSIScreen ,它为我提供了各种有用的与控制台相关的功能。

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

Under OS X and Linux, you can use the following Perl command:在 OS X 和 Linux 下,您可以使用以下 Perl 命令:

system("clear");

Don't know what the equivalent is under Windows. 不知道 Windows 下的等价物是什么。

Edit: Windows equivalent is:编辑:Windows 等效项是:

system("cls");

From perlfaq8 's answer to How do I clear the screen :perlfaq8如何清除屏幕的回答:


To clear the screen, you just have to print the special sequence that tells the terminal to clear the screen.要清除屏幕,您只需打印告诉终端清除屏幕的特殊序列。 Once you have that sequence, output it when you want to clear the screen.一旦你有了这个序列,当你想清除屏幕时输出它。

You can use the Term::ANSIScreen module to get the special sequence.您可以使用 Term::ANSIScreen 模块来获取特殊序列。 Import the cls function (or the :screen tag):导入 cls 函数(或 :screen 标签):

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

print $clear_screen;

The Term::Cap module can also get the special sequence if you want to deal with the low-level details of terminal control.如果您想处理终端控制的低级细节,Term::Cap 模块也可以获得特殊序列。 The Tputs method returns the string for the given capability: Tputs 方法返回给定功能的字符串:

use Term::Cap;

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

print $clear_screen;

On Windows, you can use the Win32::Console module.在 Windows 上,您可以使用 Win32::Console 模块。 After creating an object for the output filehandle you want to affect, call the Cls method:为要影响的输出文件句柄创建对象后,调用 Cls 方法:

use Win32::Console;

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

If you have a command-line program that does the job, you can call it in backticks to capture whatever it outputs so you can use it later:如果你有一个命令行程序来完成这项工作,你可以在反引号中调用它来捕获它输出的任何内容,以便你以后使用它:

$clear_string = `clear`;

print $clear_string;

If you are talking about a terminal, I would use something like the Curses lib to do it.如果您在谈论终端,我会使用 Curses 库之类的东西来完成它。

There is a nice Curses module to access it, which you can use like this:有一个很好的 Curses 模块可以访问它,您可以像这样使用它:

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

I disagree with the above我不同意上述

  1. Connecting additional modules = Increase the attack surface.连接额外的模块 = 增加攻击面。
  2. Reduce the Amount of Running Code.减少运行代码的数量。
  3. Code refactoring.代码重构。

#!/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 \\033 stands for ESC ANSI value 27代表 ESC ANSI 值 27
[J [J erases the screen from the current line down screen从当前行下屏幕擦除屏幕
[H [H move the cursor to the home position将光标移动到起始位置

支持 Linux 和 Windows:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM