目錄

名稱

Term::Cap - Perl termcap 介面

概要

require Term::Cap;
$terminal = Term::Cap->Tgetent({ TERM => undef, OSPEED => $ospeed });
$terminal->Trequire(qw/ce ku kd/);
$terminal->Tgoto('cm', $col, $row, $FH);
$terminal->Tputs('dl', $count, $FH);
$terminal->Tpad($string, $count, $FH);

說明

這些是從終端機功能資料庫 (termcap) 中擷取和使用功能的低階函式。

在大多數類 Unix 系統的 termcap 手冊頁中,會找到更多關於終端機功能的資訊。

方法

為了效能,Tputs 的輸出字串會快取為 1 的計數。TgotoTpad 沒有快取。$self->{_xx} 是原始的 termcap 資料,而 $self->{xx} 是快取的版本。

print $terminal->Tpad($self->{_xx}, 1);

TgotoTputsTpad 會傳回字串,如果指定,也會將字串輸出到 $FH。

Tgetent

傳回一個已加持的物件參考,使用者可以使用它來使用 TputsTgoto 將控制字串傳送到終端機。

此函式會從資料庫中擷取指定終端機類型 TERM (預設為環境變數 TERM) 的項目。

它會在環境中尋找 TERMCAP 變數。如果找到,而且其值不以斜線開頭,而且終端機類型名稱與環境字串 TERM 相同,則會使用 TERMCAP 字串,而不是讀取 termcap 檔案。如果它以斜線開頭,則該字串會用作要搜尋的 termcap 檔案的路徑名稱。如果 TERMCAP 沒有以斜線開頭,而且名稱與 TERM 不同,Tgetent 會依序搜尋檔案 $HOME/.termcap/etc/termcap/usr/share/misc/termcap,除非環境變數 TERMPATH 存在,這種情況下,它會指定一個檔案路徑名稱清單(以空格或冒號分隔),用於代替搜尋。每當搜尋多個檔案,而且在請求的項目中出現 tc 欄位時,它所命名的項目必須在同一個檔案或後續檔案中找到。如果在 TERMCAP 環境變數字串中有 :tc=...:,它會繼續在上述檔案中搜尋。

已萃取的 termcap 項目在物件中可用,為 $self->{TERMCAP}

它使用雜湊參照作為引數,並具有兩個選用金鑰

OSPEED

此終端機的終端機輸出位元率(通常錯誤地稱為波特率) - 如果未設定,將會產生警告,而且它會預設為 9600。OSPEED 可以指定為 POSIX termios/SYSV termio 速度(其中 9600 等於 9600)或舊式 DSD 風格速度(其中 13 等於 9600)。

TERM

其 termcap 項目將會使用的終端機類型 - 如果未提供,它將會預設為 $ENV{TERM}:如果未設定,則 Tgetent 會 croak。

如果失敗,它會呼叫 croak

Tpad

輸出一個文字字串,並為目前的終端機適當地填補空白。

它使用三個引數

$string

要輸出的文字字串。如果它以數字和一個選用的 '*' 開頭,則填補空白的量會根據這個數字增加,如果存在 '*',則這個量會乘以 $cnt。$string 的這個部分會在輸出之前移除。

$cnt

會用於修改套用於字串的填補空白,如上所述。

$FH

一個選用的檔案控制代碼(或 IO::Handle ),輸出會列印到其中。

填補空白後的 $string 會傳回。

Tputs

輸出給定功能的字串,適當地加上空白,且不進行任何參數替換。

它使用三個引數

$cap

要輸出的字串的功能。

$cnt

傳遞給 Tpad 的計數,用於修改套用至輸出字串的空白。如果 $cnt 為零或一,則會快取結果字串。

$FH

一個選用的檔案控制代碼(或 IO::Handle ),輸出會列印到其中。

會傳回功能的適當字串。

Tgoto

Tgoto 使用給定的參數解碼游標定址字串。

有四個參數

$cap

要輸出的功能名稱。

$col

要替換在輸出字串中的第一個值(通常是游標定址功能中的欄)。

$row

要替換在輸出字串中的第二個值(通常是游標定址功能中的列)。

$FH

輸出字串要列印到的選用檔案處理常式(或 IO::Handle )。

使用下列 sprintf() 行格式,在輸出字串中以 $col 和 $row 進行替換

%%   output `%'
%d   output value as in printf %d
%2   output value as in printf %2d
%3   output value as in printf %3d
%.   output value as in printf %c
%+x  add x to value, then do %.

%>xy if value > x then add y, no output
%r   reverse order of two parameters, no output
%i   increment by one, no output
%B   BCD (16*(value/10)) + (value%10), no output

%n   exclusive-or all parameters with 0140 (Datamedia 2500)
%D   Reverse coding (value - 2*(value%16)), no output (Delta Data)

會傳回輸出字串。

Trequire

將功能清單作為參數,如果找不到功能,則會產生 croak。

範例

use Term::Cap;

# Get terminal output speed
require POSIX;
my $termios = POSIX::Termios->new;
$termios->getattr;
my $ospeed = $termios->getospeed;

# Old-style ioctl code to get ospeed:
#     require 'ioctl.pl';
#     ioctl(TTY,$TIOCGETP,$sgtty);
#     ($ispeed,$ospeed) = unpack('cc',$sgtty);

# allocate and initialize a terminal structure
my $terminal = Term::Cap->Tgetent({ TERM => undef, OSPEED => $ospeed });

# require certain capabilities to be available
$terminal->Trequire(qw/ce ku kd/);

# Output Routines, if $FH is undefined these just return the string

# Tgoto does the % expansion stuff with the given args
$terminal->Tgoto('cm', $col, $row, $FH);

# Tputs doesn't do any % expansion.
$terminal->Tputs('dl', $count = 1, $FH);

著作權和授權

Copyright 1995-2015 (c) perl5 移植者。

此軟體為自由軟體,且可以在與 Perl 相同的條款下修改和散布。

有關 Perl 授權的詳細資訊,請參閱 Perl 原始碼散布中的 README 檔案。

作者

此模組是 Perl 核心散布的一部分,且由 Jonathan Stowe <jns@gellyfish.co.uk> 維護於 CPAN。

程式碼託管在 Github:https://github.com/jonathanstowe/Term-Cap 請隨時分岔、提交修補程式等。

另請參閱

termcap(5)