简体   繁体   中英

How to display a statement at a specfic row and column using Term::Cap module in perl

I writing a code which will display a statement at 2 nd row and 10th column using Term::Cap module in perl .

I have tried the below code

#!/apps/perl/5.8.9/bin/perl
#
use strict;
use warnings;

require POSIX;
use Term::Cap;

my $termios =new  POSIX::Termios;
$termios->getattr;
my $ospeed =$termios->getospeed;

my $terminal = Tgetent Term::Cap {TERM=>undef, OSPEED=>$ospeed};
$terminal->Trequire(qw/ce ku kd/);
$terminal->Tgoto('cm',5,2);
print "Hello World\n";
$terminal->Tputs('c1');

I am getting output without any row and column position which I specific. Also what will Trequire(), Tgetent(), do . Also is it possible to display Hello in (r1,c1) and World at (r1,c2).

My original answer was partly incorrect, I've corrected it.

The Term::Cap module is for manipulating a simple, antiquated terminal-related database. From the description:

These are low-level functions to extract and use capabilities from a terminal capability (termcap) database.

This database may not be present on a modern implementation of Unix (eg Linux). The perl module uses infocmp -C to obtain the terminal information if it cannot find the database file.

I think your example is based on the documentation which appears a little misleading with its use of $FH . The print statement in perl is simply going to standard output and will be devoid of any positioning because Tgoto returns the control characters for the positioning. It can be used for positioning in two ways:

print $terminal->Tgoto('cm',5,2);

or

$terminal->Tgoto('cm',5,2,*STDOUT);

You are likely to also want to automatically flush standard output too to avoid buffering effects, see below for a complete example which also features correct use of Trequire :

use Term::Cap;
use strict;

my $ospeed = 9600; 
my $terminal = Tgetent Term::Cap { TERM => undef, OSPEED => $ospeed };
$terminal->Trequire(qw/cl cm/);

STDOUT->autoflush(1);          ### turn off buffering
print $terminal->Tputs('cl');  ### clear screen

my ($row, $col) = (16, 10);

foreach my $word (reverse(qw(ALL YOUR TERMCAP ARE BELONG TO US))) {
  sleep(1);
  print $terminal->Tgoto('cm', $row, $col--);  ### position cursor
  print $word;
}

print $terminal->Tgoto('cm', 0, 18);

Trequire is checking the term inal you are using the cap abilities you are using in the program represented by the short termcap character codes . In the progam above: cl for clear screen and home cursor and cm for position cursor. It will throw an exception and terminate the program if the terminal does not support the specified list.

The ancient higher-level libraries in this space for unix were called curses and were used for "full-screen" commands like vi . A modern version of this is ncurses . I'd suggest looking searching for curses on CPAN to find something that has the features suitable for your use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM