tags:

views:

1266

answers:

6

Ideally, something cross-platform.

+11  A: 

The CPAN is probably the best way to go. Take a look at Term::Screen:Uni:

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

$scr->clrscr()
zigdon
+6  A: 

If you are talking about a terminal, I would use something like the Curses lib to do it.

There is a nice Curses module to access it, which you can use like this:

perl -MCurses -e '$win=new Curses;$win->clear()'
Sec
+4  A: 
system(($^O eq 'MSWin32') ? 'cls' : 'clear');

No external library required (not that there's anything wrong with CPAN or Curses).

Bill the Lizard
Not everything that isn't Windows has a clear. :)
brian d foy
Can you give an example?
Bill the Lizard
+7  A: 

I generally use Term::ANSIScreen from CPAN which gives me all sorts of useful console-related features.

use Term::ANSIScreen qw(cls);
cls();
tsee
A: 
print "\033[2J";    #clear the screen
print "\033[0;0H"; #jump to 0,0
A: 

From perlfaq8's answer to How do I clear the screen:


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;
brian d foy