看一些模块的代码,很多时候通过*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的更多相关文章

  1. 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 ...

  2. [转载]两个半小时学会Perl

    Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...

  3. Installing vSphere SDK for Perl

    Installing vSphere SDK for Perl 你可以安装vSphere SDK 在Linux 或者Microsoft Windows 系统,或者 部署 VSphere Managem ...

  4. Perl socket编程

    In this article, let us discuss how to write Perl socket programming using the inbuilt socket module ...

  5. 读写生信流程必备的 Perl 语法

    最早就是写Perl的,后来来到公司转Python,现在又要负责流程了,开始重拾Perl,当然是借鉴别人现有的语法,我再重新组合. 基本语法就不介绍了,参照我之前文章 Perl   模块 use str ...

  6. 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 ...

  7. Perl monks 的 快速回复

    on Jun 20, 2019 at 11:39 UTC ( #11101620=perlquestion: print w/replies, xml ) Need Help?? jimyokl ha ...

  8. Git - Tutorial [Lars Vogel]

    From: http://www.vogella.com/tutorials/Git/article.html Git - Tutorial Lars Vogel Version 5.6 Copyri ...

  9. Git - Tutorial官方【转】

    转自:http://www.vogella.com/tutorials/Git/article.html#git_rename_branch Lars Vogel Version 5.8 Copyri ...

随机推荐

  1. 【LeetCode练习题】Remove Duplicates from Sorted List II

    Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplica ...

  2. hdu 5641 King's Phone(暴力模拟题)

    Problem Description In a military parade, the King sees lots of new things, including an Andriod Pho ...

  3. C#Http编程

    c# 模拟 网页实现12306登陆.自动刷票.自动抢票完全篇(转) 这一篇文章,我将从头到尾教大家使用c#模拟网页面登陆12306网站,自动刷票,选择订票人,到最后一步提交订单.研究过HTTP协议的童 ...

  4. iOS开发之Crash分析,以及收集

    一  先谈谈iOS的Crash收集方式: 1. APP 发生crash,用户手机手机上肯定会有crash纪录,当然删除了该app,或是删了再装 crash纪录还是没了. 2. 如果用户设置-隐私  同 ...

  5. 多校联赛2 Problem2 Warm up 求桥的数目+缩点后的树的直径 当时被不知道原因的爆栈爆到无语了。。

    Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total S ...

  6. android CMWAP, CMNET有何差别

    什么是CMNET,什么是CMWAP? 答:CMWAP和CMNET仅仅是中国移动为其划分的两个GPRS接入方式.中国移动对CMWAP作了一定的限制,主要表如今CMWAP接入时仅仅能訪问GPRS网络内的I ...

  7. A10 平板开发一硬件平台搭建

    A10板子从原理图设计.接插件布局.PCB设计到物料采购以及贴片,最后调试,花了不少时间,刚刚把屏点亮了,系统总算跑起来了.整个过程遇到不少问题,包括与外面工程师沟通.硬件测试.软件调试,还有很多问题 ...

  8. 阿里云部署Docker(5)----管理和公布您的镜像

    出到这节,我在百度搜索了一下"阿里云部署Docker",突然发现怎么会有人跟我写的一样呢?哦,原来是其它博客系统的爬虫来抓取,然后也不会写转载自什么什么的.所以,我最终明确为什么那 ...

  9. 一个Demo就懂的Angular之directive

    <body> <div ng-controller="myCtrl"> <hello-word></hello-word> < ...

  10. Oracle - 找不到原因的无效字符

      当执行Oracle语句时,提示“无效字符”,而语句并无错误时,尝试把语句中的空格替换成半角状态的.   一般直接复制的语句会出现这种问题.