看一些模块的代码,很多时候通过*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. 【BBST 之伸展树 (Splay Tree)】

    最近“hiho一下”出了平衡树专题,这周的Splay一直出现RE,应该删除操作指针没处理好,还没找出原因. 不过其他操作运行正常,尝试用它写了一道之前用set做的平衡树的题http://codefor ...

  2. POJ 1300 欧拉通路&欧拉回路

    系统的学习一遍图论!从这篇博客开始! 先介绍一些概念. 无向图: G为连通的无向图,称经过G的每条边一次并且仅一次的路径为欧拉通路. 如果欧拉通路是回路(起点和终点相同),则称此回路为欧拉回路. 具有 ...

  3. XTU OJ 1209 Alice and Bob 2014(嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛)

    Problem Description The famous "Alice and Bob" are playing a game again. So now comes the ...

  4. [Unit Testing] Directive testing, require parent controller

    function getCompiledElement() { $scope.chart = { additional: "$ 1.56 / per minute", text: ...

  5. HDU 1085 Holding Bin-Laden Captive! (母函数)

    Holding Bin-Laden Captive! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Ja ...

  6. chart.js制作折线图

    <!DOCTYPE html> <html> <head> <title></title> </head> <script ...

  7. Linq:切勿使用 Count() > 0 来判断集合非空

    原文(http://www.cnblogs.com/ldp615/archive/2011/12/11/2284154.html) Linq 出现之前,我们通常使用下面的方式来判断集合是否非空,即集合 ...

  8. 面试前的准备---C#知识点回顾----03

    经过一天的奔波,喜忧参半,不细表 再回看下标题,C#知识点回顾 再看下内容,数据库3NF 原谅我这个标题党 今天继续回忆 1.HTTP中Post和Get区别 这忒简单了吧,大家是不是感觉到兴奋了,长舒 ...

  9. 关于arm-linux-gcc的安装与配置

    在嵌入式开发中我们经常会用到arm-linux-gcc来编译我们的应用程序.作为arm-linux-gcc的入门,我们先看看如何安装arm-linux-gcc. 安装arm-linux-gcc还是比较 ...

  10. (原)ubuntu16中编译boost1.61.0库

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5797940.html 参考网址: http://www.boost.org/doc/libs/1_61 ...