简体   繁体   English

如何在 perl 中使用 Term::Cap 模块在特定行和列显示语句

[英]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 .我编写了一个代码,该代码将使用 perl 中的 Term::Cap 模块在第 2 行和第 10 列显示一条语句。

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 .还有 Trequire()、Tgetent() 会做什么。 Also is it possible to display Hello in (r1,c1) and World at (r1,c2).也可以在 (r1,c1) 中显示 Hello,在 (r1,c2) 中显示 World。

My original answer was partly incorrect, I've corrected it.我原来的答案有部分不正确,我已经更正了。

The Term::Cap module is for manipulating a simple, antiquated terminal-related database. Term::Cap模块用于操作一个简单的、过时的终端相关数据库。 From the description:从描述来看:

These are low-level functions to extract and use capabilities from a terminal capability (termcap) database.这些是从终端功能 (termcap) 数据库中提取和使用功能的低级函数。

This database may not be present on a modern implementation of Unix (eg Linux).该数据库可能不存在于 Unix(例如 Linux)的现代实现中。 The perl module uses infocmp -C to obtain the terminal information if it cannot find the database file. perl 模块在找不到数据库文件时使用infocmp -C获取终端信息。

I think your example is based on the documentation which appears a little misleading with its use of $FH .我认为您的示例基于文档,该文档在使用$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. perl 中的print语句只是进入标准输出,并且没有任何定位,因为Tgoto返回定位的控制字符。 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 :您可能还希望自动刷新标准输出以避免缓冲效果,请参阅下面的完整示例,该示例还具有正确使用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 . Trequire是伊纳勒您使用您使用的是由短所表示的节目能力检查术语的termcap字符代码 In the progam above: cl for clear screen and home cursor and cm for position cursor.在上面的程序中: cl表示清除屏幕和主页光标, cm表示位置光标。 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 .这个空间中用于 unix 的古老的高级库被称为 curses,用于“全屏”命令,如vi A modern version of this is ncurses .这个的现代版本是ncurses I'd suggest looking searching for curses on CPAN to find something that has the features suitable for your use case.我建议在CPAN上搜索curses,以找到具有适合您用例的功能的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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