當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Perl untie用法及代碼示例



描述

該函數打破了變量和包之間的綁定,撤銷了由 tie 函數創建的關聯。

用法

以下是此函數的簡單語法 -

untie VARIABLE

返回值

此函數在失敗時返回 0,成功時返回 1。

示例

以下是顯示其基本用法的示例代碼 -

#!/usr/bin/perl -w

package MyArray;

sub TIEARRAY {
   print "TYING\n";
   bless [];
}

sub DESTROY {
   print "DESTROYING\n";
}

sub STORE {
   my ($self, $index, $value ) = @_;
   print "STORING $value at index $index\n";
   $self[$index] = $value;
}

sub FETCH {
   my ($self, $index ) = @_;
   print "FETCHING the value at index $index\n";
   return $self[$index];
}

package main;
$object = tie @x, MyArray; #@x is now a MyArray array;

print "object is a ", ref($object), "\n";

$x[0] = 'This is test'; #this will call STORE();
print $x[0], "\n";      #this will call FETCH();
print $object->FETCH(0), "\n";
untie @x    		#now @x is a normal array again.

執行上述代碼時,會產生以下結果 -

TYING
object is a MyArray
STORING This is test at index 0
FETCHING the value at index 0
This is test
FETCHING the value at index 0
This is test
DESTROYING

相關用法


注:本文由純淨天空篩選整理自 Perl untie Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。