tie VARIABLE,CLASSNAME,LIST

此函式會將變數繫結至套件類別,而該類別會提供變數的實作。VARIABLE 為要套用變化的變數名稱。CLASSNAME 為實作正確類型物件的類別名稱。任何其他引數都會傳遞給類別的適當建構函式方法(意即 TIESCALARTIEHANDLETIEARRAYTIEHASH)。通常這些引數就像傳遞給 C 的 dbm_open(3) 函式一樣。建構函式傳回的物件也會由 tie 函式傳回,如果您想要存取 CLASSNAME 中的其他方法,這會很有用。

請注意,當用於大型物件(例如 DBM 檔案)時,諸如 keysvalues 等函式可能會傳回龐大的清單。您可能偏好使用 each 函式來反覆運算這些物件。範例

# print out history file offsets
use NDBM_File;
tie(my %HIST, 'NDBM_File', '/usr/lib/news/history', 1, 0);
while (my ($key,$val) = each %HIST) {
    print $key, ' = ', unpack('L', $val), "\n";
}

實作雜湊的類別應具有下列方法

TIEHASH classname, LIST
FETCH this, key
STORE this, key, value
DELETE this, key
CLEAR this
EXISTS this, key
FIRSTKEY this
NEXTKEY this, lastkey
SCALAR this
DESTROY this
UNTIE this

實作一般陣列的類別應具備下列方法

TIEARRAY classname, LIST
FETCH this, key
STORE this, key, value
FETCHSIZE this
STORESIZE this, count
CLEAR this
PUSH this, LIST
POP this
SHIFT this
UNSHIFT this, LIST
SPLICE this, offset, length, LIST
EXTEND this, count
DELETE this, key
EXISTS this, key
DESTROY this
UNTIE this

實作檔案句柄的類別應具備下列方法

TIEHANDLE classname, LIST
READ this, scalar, length, offset
READLINE this
GETC this
WRITE this, scalar, length, offset
PRINT this, LIST
PRINTF this, format, LIST
BINMODE this
EOF this
FILENO this
SEEK this, position, whence
TELL this
OPEN this, mode, LIST
CLOSE this
DESTROY this
UNTIE this

實作純量的類別應具備下列方法

TIESCALAR classname, LIST
FETCH this,
STORE this, value
DESTROY this
UNTIE this

並非所有上述方法都需要實作。請參閱 perltieTie::HashTie::ArrayTie::ScalarTie::Handle

dbmopen 不同,tie 函式不會替您 userequire 模組;您需要自行明確執行。請參閱 DB_FileConfig 模組,以取得有趣的 tie 實作。

如需進一步詳細資訊,請參閱 perltietied