Perl Symbolic Reference
看一些模块的代码,很多时候通过*glob的方式来改变变量或者函数,这种方法称为Symbolic reference。
首先看一下*glob的结构,这个在之前的博文已经讲过,不做细述:
SV = PVGV(0x18d1364) at 0x182aaec
REFCNT = 2
FLAGS = (IN_PAD)
NAME = "test"
NAMELEN = 4
GvSTASH = 0x298fc4 "main"
GP = 0x18b90ac
SV = 0x0
REFCNT = 1
IO = 0x0
FORM = 0x0
AV = 0x0
HV = 0x0
CV = 0x0
CVGEN = 0x0
LINE = 4
FILE = "test.pl"
FLAGS = 0x8
EGV = 0x182aaec "test"
运行下边的代码段,察看一下输出对应的ref slot。
print ref \1;
print ref \'testing';
print ref [qw(one, two ,three)];
print ref {};
print ref sub {};
如何给*glob赋值?通过下面的方式,填充Symbol的不同slot,从而使得对应的$test,@test,%test,&test非空。
use Data::Dumper;
use Devel::Peek; *test = \1;
*test = [qw(one two three)];
*test = {qw(one two three four)};
*test = sub {}; print Dump *test;
SV = PVGV(0x18d134c) at 0x182aaec
REFCNT = 6
FLAGS = (MULTI,ASSUMECV,IN_PAD)
NAME = "test"
NAMELEN = 4
GvSTASH = 0x298fc4 "main"
GP = 0x18b9104
SV = 0x182aadc
REFCNT = 1
IO = 0x0
FORM = 0x0
AV = 0x2990d4
HV = 0x299214
CV = 0x18b1e7c
CVGEN = 0x0
LINE = 4
FILE = "test.pl"
FLAGS = 0xe
EGV = 0x182aaec "test"
如何使用?use strict情况下有几种方式:
下面的情况直接调用Symbol或者通过our定义直接访问变量,函数无需our定义,总是一个Symbol,可以直接通过名字访问。
use strict;
use Data::Dumper;
use Devel::Peek; *test = \1;
*test = [qw(one two three)];
*test = {qw(one two three four)};
*test = sub {print "testing\n";}; print $::test;
print %::test;
&test(); our ($test, %test);
print $test;
print %test;
&test;
如果通过一个变量传名字访问另外一个变量,则需要通过下面的方式no strict 'refs'。
no strict 'refs';
print ${'test'};
print %{'test'};
&{'test'};
Perl Symbolic Reference的更多相关文章
- error: bad symbolic reference. A signature in HiveContext.class refers to term hive
在spark-shell中执行val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc)报错: error: bad symbolic ...
- [转载]两个半小时学会Perl
Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...
- Installing vSphere SDK for Perl
Installing vSphere SDK for Perl 你可以安装vSphere SDK 在Linux 或者Microsoft Windows 系统,或者 部署 VSphere Managem ...
- Perl socket编程
In this article, let us discuss how to write Perl socket programming using the inbuilt socket module ...
- 读写生信流程必备的 Perl 语法
最早就是写Perl的,后来来到公司转Python,现在又要负责流程了,开始重拾Perl,当然是借鉴别人现有的语法,我再重新组合. 基本语法就不介绍了,参照我之前文章 Perl 模块 use str ...
- Quick Reference Card Urls For Web Developer
C# C# Cheatsheet & Notes Coding Guidelines for C# 3.0, 4.0, 5.0 Core C# and .NET Quick Reference ...
- Perl monks 的 快速回复
on Jun 20, 2019 at 11:39 UTC ( #11101620=perlquestion: print w/replies, xml ) Need Help?? jimyokl ha ...
- Git - Tutorial [Lars Vogel]
From: http://www.vogella.com/tutorials/Git/article.html Git - Tutorial Lars Vogel Version 5.6 Copyri ...
- Git - Tutorial官方【转】
转自:http://www.vogella.com/tutorials/Git/article.html#git_rename_branch Lars Vogel Version 5.8 Copyri ...
随机推荐
- mysql 基础技术
一.树状结构 参考http://www.cnblogs.com/kingteach/archive/2011/07/05/2098046.html )) begin declare lev int; ...
- [工作问题总结]MyEclipse 打开项目
------------------------------ASP.Net+Android+IO开发 .Net培训 期待与您交流!------------------------------ 1.项目 ...
- 4. 绘制光谱曲线QGraphicsView类
一.前言 Qt的QGraphicsView类具有强大的视图功能,与其一起使用的还有QGraphicsScene类和QGraphicsItem类.大体思路就是通过构建场景类,然后向场景对象中增加各种图元 ...
- openssl 对称加密算法enc命令详解
1.对称加密算法概述 openssl的加密算法库提供了丰富的对称加密算法,我们可以通过openssl提供的对称加密算法指令的方式使用,也可以通过调用openssl提供的API的方式使用. openss ...
- [Python]打开文件的模式
Python中以sys.open()方法打开文件 import sys file = open("D:\\file.txt") 其中可在第二个参数的位置指定打开文件的模式 impo ...
- 基础命名空间:反射 using System.Reflection
反射概念: .Net的应用程序由几个部分:‘程序集(Assembly)’.‘模块(Module)’.‘类型(class)’组成,程序集包含模块 模块包含类型,类型又包含 成员,而反射提供一 ...
- PHP学习笔记三十五【Try】
<?php function AddUser($name) { if($name=="张三") { echo "add success"; return ...
- oracle索引学习
查看执行状态: 选中代码直接按F5,或者点击Tools===>>Explain Plan 一.索引的注意事项: 当任何单个查询要检索的行少于或者等于整个表行数的10%时,索引就非常有用.这 ...
- WCF入门教程系列一
一.概述 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,可以翻译为Windows通讯接口,它是.NET框架的一部分.由 .NE ...
- C++学习笔录4
1.容器=数据结构+算法.相当于是为复杂的数据设计一种专门用于存放该数据的东西.用于开发中传递复杂的数据. 2.模版函数只能写在头文件中.不能单独做声明. 3.STL容器类分为三类: (1).顺序容器 ...