简体   繁体   English

为什么undef值在Perl中成为有效的数组引用?

[英]Why does an undef value become a valid array reference in Perl?

In perl 5.8.5, if I do the following, I don't get an error: 在perl 5.8.5中,如果我执行以下操作,则不会出现错误:

use strict;

my $a = undef;
foreach my $el (@$a) {
  ...whatever
}

What's going on here? 这里发生了什么? Printing out the output of ref($a) shows that $a changes to become a valid array reference at some point. 打印出ref($a)的输出表明$a更改为在某个时刻成为有效的数组引用。 But I never explicitly set $a to anything. 但是我从未明确地将$a设置为任何东西。

Seems kind of odd that the contents of a variable could change without me doing anything. 似乎有点奇怪,如果没有我做任何事情,变量的内容可能会改变。

Thoughts, anyone? 思绪,有人吗?

EDIT: Yes, I know all about auto-vivification. 编辑:是的,我知道所有关于自动生存的知识。 I always thought that there had to be a assignment somewhere along the way to trigger it, not just a reference. 我一直认为必须在某个地方有一个任务来触发它,而不仅仅是一个参考。

Auto-vivification is the word. 自动生存就是这个词。 From the link: 从链接:

Autovivification is a distinguishing feature of the Perl programming language involving the dynamic creation of data structures. 自动生成是Perl编程语言的一个显着特征,涉及动态创建数据结构。 Autovivification is the automatic creation of a variable reference when an undefined value is dereferenced. 自动生成是在取消引用未定义值时自动创建变量引用。 In other words, Perl autovivification allows a programmer to refer to a structured variable, and arbitrary sub-elements of that structured variable, without expressly declaring the existence of the variable and its complete structure beforehand. 换句话说,Perl autovivification允许程序员引用结构化变量和该结构化变量的任意子元素,而不事先明确声明变量及其完整结构的存在。

In contrast, other programming languages either: 1) require a programmer to expressly declare an entire variable structure before using or referring to any part of it; 相反,其他编程语言要么:1)要求程序员在使用或引用它的任何部分之前明确地声明整个变量结构; or 2) require a programmer to declare a part of a variable structure before referring to any part of it; 或者2)要求程序员在引用变量结构的任何部分之前声明它的一部分; or 3) create an assignment to a part of a variable before referring, assigning to or composing an expression that refers to any part of it. 或3)在引用,分配或组成引用其任何部分的表达式之前,为变量的一部分创建赋值。

Perl autovivication can be contrasted against languages such as Python, PHP, Ruby, JavaScript and all the C style languages. Perl autovivication可以与Python,PHP,Ruby,JavaScript和所有C风格语言等语言进行对比。

Auto-vivification can be disabled with no autovivification; 可以禁用自动生成而无需自动no autovivification;

Read Uri Guttman's article on autovivification . 阅读Uri Guttman关于自动化的文章

There is nothing odd about it once you know about it and saves a lot of awkwardness. 一旦你了解它并节省了许多尴尬,就没有什么奇怪的了。

Perl first evaluates a dereference expression and sees that the current reference value is undefined. Perl首先计算解除引用表达式,并看到当前引用值未定义。 It notes the type of dereference (scalar, array or hash) and allocates an anonymous reference of that type. 它记录了解除引用的类型(标量,数组或散列)并分配了该类型的匿名引用。 Perl then stores that new reference value where the undefined value was stored. 然后Perl将新参考值存储在存储未定义值的位置。 Then the dereference operation in progress is continued. 然后继续进行中的解除引用操作。 If you do a nested dereference expression, then each level from top to bottom can cause its own autovivication. 如果执行嵌套的解除引用表达式,则从上到下的每个级别都可以导致其自身的自动生成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM