Perl 中的 untie函数

首页 / Perl入门教程 / Perl 中的 untie函数

描述

此函数打破了变量和包之间的绑定,从而取消了tie函数创建的关联。

语法

以下是此函数的简单语法-

无涯教程网

untie VARIABLE

返回值

如果失败,此函数返回0,如果成功,则返回1。

链接:https://www.learnfk.comhttps://www.learnfk.com/perl/perl-untie.html

来源:LearnFk无涯教程网

以下是显示其基本用法的示例代码-

#!/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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

微服务架构实战160讲 -〔杨波〕

面试现场 -〔白海飞〕

软件工程之美 -〔宝玉〕

编译原理之美 -〔宫文学〕

后端技术面试 38 讲 -〔李智慧〕

全链路压测实战30讲 -〔高楼〕

网络排查案例课 -〔杨胜辉〕

React Native 新架构实战课 -〔蒋宏伟〕

深入浅出可观测性 -〔翁一磊〕

好记忆不如烂笔头。留下您的足迹吧 :)