Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear the screen in a terminal using 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.

like image 690
perlfaq Avatar asked Jan 05 '11 15:01

perlfaq


People also ask

What command is used to clear up the terminal window?

A common way to do that is to use the `clear` command, or its keyboard shortcut CTRL+L.

How do you clear the terminal screen in Ruby?

You can use system("clear") or system("cls") according to the terminal you are going to print. If you are using the Windows Command Prompt, use system("cls") . If you are using a Mac or Linux system, just use system("clear") .


1 Answers

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;
like image 187
2 revs, 2 users 96% Avatar answered Oct 18 '22 20:10

2 revs, 2 users 96%