16

Ideally, something cross-platform.

raldi
  • 21,344
  • 33
  • 76
  • 86

8 Answers8

38
print "\033[2J";    #clear the screen
print "\033[0;0H"; #jump to 0,0
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
15

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
  • 14,573
  • 6
  • 35
  • 54
11

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

use Term::ANSIScreen qw(cls);
cls();
tsee
  • 5,034
  • 1
  • 19
  • 27
8

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:

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;
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
brian d foy
  • 129,424
  • 31
  • 207
  • 592
7

Under OS X and Linux, you can use the following Perl command:

system("clear");

Don't know what the equivalent is under Windows.

Edit: Windows equivalent is:

system("cls");
Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Sebastian M
  • 71
  • 1
  • 1
6

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
  • 7,059
  • 6
  • 31
  • 58
2

Support Linux and Windows:

system($^O eq 'MSWin32'?'cls':'clear');
Sean Lin
  • 51
  • 3
1

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 stands for ESC ANSI value 27
[J erases the screen from the current line down screen
[H move the cursor to the home position
haxa
  • 11
  • 2
  • Great answer, although I would remove the unnecessary `$over` part from the actual code in your answer. They're good motivations to just print `$cleaning` to screen, but not part of the real solution. – joanis Mar 17 '21 at 18:06
  • 1
    Сharm Perl is that - «There’s more than one way to do it» ;) – haxa Mar 17 '21 at 23:11