當前位置:首頁 » Perl » perl untie()函數

perl untie()函數

perl untie()函數例子,untie()函數實例代碼 - 打破之間的綁定變量和包,撤消關聯創建的約束作用。

語法

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];
}
#by www.gitbook.net
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