看一些模块的代码,很多时候通过*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. (转载) C/C++编译和链接过程详解 (重定向表,导出符号表,未解决符号表)

    转载http://blog.csdn.net/neo_ustc/article/details/9024839 有 些人写C/C++(以下假定为C++)程序,对unresolved external ...

  2. Ubuntu 配置Tomcat环境

    1.下载Tomcat http://tomcat.apache.org/,下载Tomcat 8(由于目前最新eclipse不支持tomcat 9) 将下载的apache-tomcat-8.0.35.t ...

  3. 【转】vlc android 代码编译

    转自:http://blog.csdn.net/asircao/article/details/7734201 系统:ubuntu12.04代码:git://git.videolan.org/vlc- ...

  4. [Hapi.js] View engines

    View engines, or template engines, allow you to maintain a clean separation between your presentatio ...

  5. iOS8 Core Image In Swift:更复杂的滤镜

    iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift:人脸 ...

  6. IOS基于新浪微博开放平台微博APP

    1.基于新浪微博开放平台APP源码 2.gitHub源代码下载地址 https://github.com/whzhaochao/SinaWeiBoOpen 3.用到的第三放开源库 3.1  RTLab ...

  7. jquery文本框验证字符长度和只能输入数字

    <input type="text" class="chujia" onkeyup="this.value=this.value.replace ...

  8. C#解决MDI窗体闪屏的方法

    最近从师兄手上接了一个C#的项目,需要用到MDI窗体,可是每当我显示子窗体的时候会有一次“闪烁”,很明显,看起来非常不爽,查找许久,知道是每次在show()子窗体的时候都会调用子窗体构造函数重绘窗体, ...

  9. hdu3081 Marriage Match II(最大流)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Marriage Match II Time Limit: 2000/1000 M ...

  10. setTimeout的时间设为0的问题

    javascript是单线程执行的,当某一段代码正在执行的时候,所有的后续任务都必须等待,形成一个队列, 一旦当前任务执行完毕,再从队列中取出下一个任务.这常被称为”阻塞式执行“. 如果代码中设定一个 ...