Perl Cookbook

Perl CookbookSearch this book
Previous: 15.2. Testing Whether a Program Is Running InteractivelyChapter 15
User Interfaces
Next: 15.4. Determining Terminal or Window Size
 

15.3. Clearing the Screen

Problem

You want to clear the screen.

Solution

Use the Term::Cap module to send the appropriate character sequence. Use POSIX Termios to get the output speed of the terminal (or guess 9600 bps). Use eval to trap errors that may arise using POSIX Termios::

use Term::Cap;

$OSPEED = 9600;
eval {
    require POSIX;
    my $termios = POSIX::Termios->new();
    $termios->getattr;
    $OSPEED = $termios->getospeed;
};

$terminal = Term::Cap->Tgetent({OSPEED=>$OSPEED});
$terminal->Tputs('cl', 1, STDOUT);

Or, just run the clear command:

system("clear");

Discussion

If you clear the screen a lot, cache the return value from the termcap or clear command:

$clear = $terminal->Tputs('cl');
$clear = `clear`;

Then you can clear the screen a hundred times without running clear a hundred times:

print $clear;

See Also

Your system's clear (1) and termcap (5) manpages (if you have them); the documentation for the standard module Term::Cap module, also in Chapter 7 of Programming Perl; the documentation for the Term::Lib module from CPAN


Previous: 15.2. Testing Whether a Program Is Running InteractivelyPerl CookbookNext: 15.4. Determining Terminal or Window Size
15.2. Testing Whether a Program Is Running InteractivelyBook Index15.4. Determining Terminal or Window Size

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.