如何使用 Perl 清除终端中的屏幕?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是官方常见问题解答,减去任何后续编辑。
要清除屏幕,您只需打印告诉终端清除屏幕的特殊序列。获得该序列后,当您想要清除屏幕时输出它。
您可以使用 Term::ANSIScreen 模块来获取特殊序列。导入
cls
函数(或:screen
标记):Term::Cap 模块还可以获取特殊序列,如果你想处理终端控制的低级细节。 Tputs 方法返回给定功能的字符串:
在 Windows 上,您可以使用 Win32::Console 模块。为您想要影响的输出文件句柄创建对象后,调用 Cls 方法:
如果您有一个可以完成这项工作的命令行程序,您可以在反引号中调用它来捕获它输出的任何内容,以便您可以稍后使用它:
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):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:
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: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:
对我有用的最短的独立于操作系统(并且不需要安装额外的模块)的方法是在 Perl Monks 线程 中找到的(该页面还包含清除屏幕的其他一些变体):
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):
Linux 用户使用以下命令:
Linux Users use the following command: