perltermcapterminfo

How can I send clear or reset with Term::Cap?


When I output tput clear | hexdump -c I get different results if I'm on kitty or xterm. How can I use Term::Cap to generate these terminal signals on the respective terminal?

What I've tried is a direct-copy-paste from the docs with setup,

use strict;
use warnings;
use Term::Cap;
use POSIX;
my $termios = new POSIX::Termios;
$termios->getattr;
my $ospeed = $termios->getospeed;

my $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };

And then I thought this should work,

$terminal->Tputs('clear', 1, *STDOUT );

But alas, it does nothing.

If I provide a different non-existent name for the term (rather than undef which defaults to $ENV{TERM}, I get)

Can't find a valid termcap file at ./test.pl line 9.

So I know it's looking up the termcap file, and finding it.


Solution

  • Right way

    All of termcap's signal names are two letters. For clear you'll want cl

    $terminal->Tputs('cl', 1, *STDOUT );
    

    You can find the full list on man termcap, which has a full list:

    ch   Move cursor horizontally only to column %1
    cl   Clear screen and cursor home
    cm   Cursor move to row %1 and column %2 (on screen)
    

    Thanks to Thomas Dickey for the answer in comments

    Wrong way

    Not sure what I was doing wrong, as a temporary work around I did

    use constant CLEAR => do {
        open( my $fh, '-|', qw(tput clear) );
        scalar <$fh>;
    };
    

    This still has a spin up another process, but it worked fine. I won't accept this answer in the event anyone knows how to do this the right way.