perl 变量详解
http://www.perlmonks.org/?node_id=933450 use strict;
use Devel::Peek; my $a;
Dump($a);
$a=4;
Dump($a);
$a eq "13";
Dump($a);
$a++;
Dump($a);
print $a;
Dump($a); ##</code><code>## C:\Users\ian>perl test.pl
SV = NULL(0x0) at 0x1d9ec1c
REFCNT = 1
FLAGS = (PADMY)
SV = IV(0x1d9ec18) at 0x1d9ec1c
REFCNT = 1
FLAGS = (PADMY,IOK,pIOK)
IV = 4
SV = PVIV(0x1d91c34) at 0x1d9ec1c
REFCNT = 1
FLAGS = (PADMY,IOK,POK,pIOK,pPOK)
IV = 4
PV = 0x1dbdf84 "4"\0
CUR = 1
LEN = 4
SV = PVIV(0x1d91c34) at 0x1d9ec1c
REFCNT = 1
FLAGS = (PADMY,IOK,pIOK)
IV = 5
PV = 0x1dbdf84 "4"\0
CUR = 1
LEN = 4
SV = PVIV(0x1d91c34) at 0x1d9ec1c
REFCNT = 1
FLAGS = (PADMY,IOK,POK,pIOK,pPOK)
IV = 5
PV = 0x1dbdf84 "5"\0
CUR = 1
LEN = 4
5 ##</code><code>## SV = NULL(0x0) at 0x1d9ec1c
REFCNT = 1
FLAGS = (PADMY) ##</code><code>## SV = IV(0x1d9ec18) at 0x1d9ec1c
REFCNT = 1
FLAGS = (PADMY,IOK,pIOK)
IV = 4 ##</code><code>## SV = PVIV(0x1d91c34) at 0x1d9ec1c
REFCNT = 1
FLAGS = (PADMY,IOK,POK,pIOK,pPOK)
IV = 4
PV = 0x1dbdf84 "4"\0
CUR = 1
LEN = 4 ##</code><code>## SV = PVIV(0x1d91c34) at 0x1d9ec1c
REFCNT = 1
FLAGS = (PADMY,IOK,pIOK)
IV = 5
PV = 0x1dbdf84 "4"\0
CUR = 1
LEN = 4 ##</code><code>## SV = PVIV(0x1d91c34) at 0x1d9ec1c
REFCNT = 1
FLAGS = (PADMY,IOK,POK,pIOK,pPOK)
IV = 5
PV = 0x1dbdf84 "5"\0
CUR = 1
LEN = 4
http://blob.perl.org/tpc/1998/Perl_Language_and_Modules/Perl%20Illustrated/ presents some of the details of how perl stores and keeps track of variables and their values. It is a bit more complex than the simple model presended in perldata and the commonly used terms "variable" and "value". There are several data structures used and linked to each other in the case of most variables. Depending on the history of the variable, there will be various bits of memory allocated and with more or less current values.
You can follow the evolution of $a at each step in your program by adding a few more Dump calls:
Which produces:
The first Dump produces:
At this point, the variable is declared but no value has been assigned. There is a root data structure allocated: SV, but no memory allocated to store a value: the value is undef and a NULL pointer value in the SV structure is sufficient to record this.
After assigning an integer value to the variable, Dump produces:
Here an IV structure has been allocated and the assigned value, 4, has been stored in it. The pointer in the SV structure now points to the IV structure: it is not a NULL pointer any more. Flags are set to indicate that the variable now contains a valid integer value (IOK and pIOK).
Next you do a string comparison, for which perl needs to convert the variable value to a string. After this Dump shows:
The string value could be created, used then discarded, but instead perl updates the stored value, keeping the string value in case it might be used again. The integer value is still correct, so the variable now has two values: an integer value and a string value. Two data structures are used: the original SV and a PVIV structure which contains the integer value and a pointer to the string value. And the flags are updated to indicate that there are valid integer and string values.
Your next operation is to increment $a. This is an operation on its integer value. In this case, perl is frugal and does only enough work to record the result. The integer value is changed but the string value is not changed. Rather, the flags indicating a valid string value are turned off. After this, Dump produces:
Note that perl does not bother to change the PVIV structure back to an IV structure. The latter is smaller but the saving of memory would be tiny - not enough to justify the work. So, the PVIV structure remains, and it still points to the memory allocated to hold the string value, which still has the old, but no longer correct string value of the variable. It's OK that the string value is incorrect because the POK and pPOK flags are off, so the value will not be used. This wastes some memory, but saves some CPU.
Then you print the value of $a. Again, a string value is needed. While the PVIV structure has a pointer to a string value, the POK and pPOK flags in the SV are off, so this value isn't used. Instead, perl again converts the current integer value to a string and prints that correct value. And, again, perl saves this value in case it might be used again. So, after printing Dump produces:
Again there are both integer and string values, and they are current and consistent. The IOK, POK, pIOK and pPOK flags are all set so that in subsequent operations these value might be used.
Note also that the new string value is in the same bit of memory that the original string value was in. No additional memory was needed, so no new memory was allocated. The original memory was re-used. A lot of work has gone into minimizing expensive operations for the sake of performance.
You might try extending your program, assigning a long string value to $a and then dumping it again. If you did, you would see that the same SV and PVIV structures were in use but the IOK and pIOK flags were off and the integer value in the PVIV remained unchanged and the long string was contained in newly allocated memory, as it wouldn't fit in the block originally allocated to hold the previous single character value.
perl 变量详解的更多相关文章
- mysql show variables系统变量详解
mysql系统变量详解 mysqld服务器维护两种变量.全局变量影响服务器的全局操作.会话变量影响具体客户端连接相关操作. 服务器启动时,将所有全局变量初始化为默认值.可以在选项文件或命令行中指定的选 ...
- net core体系-web应用程序-4net core2.0大白话带你入门-5asp.net core环境变量详解
asp.net core环境变量详解 环境变量详解 Windows操作系统的环境变量在哪设置应该都知道了. Linux(centos版本)的环境变量在/etc/profile里面进行设置.用户级的 ...
- Maya 常用环境变量详解
Maya 常用环境变量详解 前言: Maya 的环境变量让用户可以很方便的自定义 Maya 的功能. 在 Maya 的 Help 帮助文档中有专门的一个章节< Environment Varia ...
- Shell学习之Bash变量详解(二)
Shell学习之Bash变量详解 目录 Bash变量 Bash变量注意点 用户自定义变量 环境变量 位置参数变量 预定义变量 Bash变量 用户自定义变量:在Bash中由用户定义的变量. 环境变量:这 ...
- asp.net core环境变量详解
环境变量详解 Windows操作系统的环境变量在哪设置应该都知道了. Linux(centos版本)的环境变量在/etc/profile里面进行设置.用户级的环境变量在其它文件里面,不多说了,有兴趣的 ...
- 4、Ubuntu系统环境变量详解
参考:Linux公社Ubuntu系统环境变量详解 UNIX/Linux系统中的环境变量和库文件的使用方法 由于Linux系统严格的权限管理,造成Ubuntu系统有多个环境变量配置文件,因此我们需要了解 ...
- Nginx基础篇(2)- Nginx基本配置文件和变量详解
Nginx基本配置文件和变量详解 1. 基本配置文件 /etc/nginx/nginx.conf # nginx运行的用户 user nginx; # nginx进程数,建议设置为等于CPU总核心数. ...
- 如何查找YUM安装的JAVA_HOME环境变量详解
如何查找YUM安装的JAVA_HOME环境变量详解 更新时间:2017年10月27日 09:44:56 作者:铁锚 我要评论 这篇文章主要给大家介绍了关于如何查找YUM安装的JAVA_HOM ...
- Python变量详解
Python变量详解 目录 Python变量详解 一.变量的定义 二.变量组成的三部分 2.1 变量名 2.1.1 变量名的命名规范 2.1.2 变量名的命名风格 2.2 赋值符号 2.3 变量值 三 ...
随机推荐
- Ubuntu 12.04中文输入法的安装
Ubuntu 12.04中文输入法的安装 Ubuntu上的输入法主要有小小输入平台(支持拼音/二笔/五笔等),Fcitx,Ibus,Scim等.其中Scim和Ibus是输入法框架. 在Ubuntu ...
- Tomcat中的线程池StandardThreadExecutor
之所以今天讨论它,因为在motan的的NettyServer中利用它这个线程池可以作为业务线程池,它定制了一个自己的线程池.当然还是基于jdk中的ThreadExecutor中的构造方法和execut ...
- aggregation(2):adaptive Boosting (AdaBoost)
给你这些水果图片,告诉你哪些是苹果.那么现在,让你总结一下哪些是苹果? 1)苹果都是圆的.我们发现,有些苹果不是圆的.有些水果是圆的但不是苹果, 2)其中到这些违反"苹果都是圆的" ...
- WinDriver&PCIE
1.安装VS2012 安装VS2012略过,主要用它来做数据传输应用程序的,WINDRIVER提供了一系列API接口,方便了用户,使用户能直接进入用户态的编程,因为内核态的编程它已做好,不需要进行修改 ...
- 瞬间从IT屌丝变大神——注释规则
注释的主要规则如下: 公共组件和各栏目的维护者都需要在文件头部加上注释说明: /** *文件用途说明 *作者姓名 *联系方式*制作日期 **/ 大的模块注释方法: //======= //代码用途 / ...
- hadoop测试环境主配置简例
1,mapred-site.xml 此配置文件主要是针对mapreduce的配置文件,配置的是jobtracker的地址和端口; <configuration> <property& ...
- 第二百六十一、二天 how can I坚持
昨天在家宅了一天,晚上去看了个电影<寻龙诀>,感觉一般,虽然有黄渤.舒淇.十分打7分吧,画面倒是很震撼. 今天,去了趟东升八家郊野公园,想买个篮球玩玩来,可是超市里的太坑了,都是赠品还拿出 ...
- Red5点播和直播的实现
(一) Red5流媒体服务器介绍Red5是一个采用Java开发开源的Flash流媒体服务器.它支持:把音频(MP3)和视频(FLV)转换成播放流: 录制客户端播放流(只支持FLV):共享 ...
- iOS学习之触摸事件
触摸事件 iOS中的事件: 在用户使用app过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: view的触摸事件处理: 响应者对象: 在iOS中不是任何对象都能处理事件,只有继承了UIR ...
- 【C语言】-循环的嵌套
循环的嵌套:当在一个循环语句中嵌入另一个循环时,成为循环的嵌套. 循环嵌套的形式: (1)for语句中嵌入for语句: for ( ) { for ( ) { ... } } (2)for语句嵌入wh ...