如何使用 Perl 清除终端中的屏幕?

发布于 2024-10-10 10:55:56 字数 273 浏览 0 评论 0原文

我想使用 Perl 清除终端/控制台中的屏幕。我怎样才能做到这一点?


这是来自官方 perlfaq 的问题。我们正在将 perlfaq 导入 Stack Overflow

I would like to clear the screen in a terminal/console using Perl. How can I do that?


This is a question from the official perlfaq. We're importing the perlfaq to Stack Overflow.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

星光不落少年眉 2024-10-17 10:55:56

这是官方常见问题解答,减去任何后续编辑。

要清除屏幕,您只需打印告诉终端清除屏幕的特殊序列。获得该序列后,当您想要清除屏幕时输出它。

您可以使用 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 方法:

Win32::Console;

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

print $clear_screen;

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

$clear_string = `clear`;

print $clear_string;

This is the offical FAQ answer minus any subsequent edits.

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. Import the cls function (or the :screen tag):

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. The Tputs method returns the string for the given capability:

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. After creating an object for the output filehandle you want to affect, call the Cls method:

Win32::Console;

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

print $clear_screen;

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;
宁愿没拥抱 2024-10-17 10:55:56

对我有用的最短的独立于操作系统(并且不需要安装额外的模块)的方法是在 Perl Monks 线程 中找到的(该页面还包含清除屏幕的其他一些变体):

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

The shortest OS-independent (and not needing additional modules installations) method that worked for me was found in the Perl Monks thread (that page also contains some other variations of clearing the screen):

system $^O eq 'MSWin32' ? 'cls' : 'clear';
不语却知心 2024-10-17 10:55:56

Linux 用户使用以下命令:

system 'clear';

Linux Users use the following command:

system 'clear';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文