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_function_references.htm