Perl 中的引用是如何工作的 [英] How do references in Perl work
问题描述
谁能向我解释以下 Perl 代码中的 push
语句?我知道 perl 中的 push 是如何工作的,但我无法理解以下 push 命令中的第一个参数代表什么.我正在尝试解释某人的脚本.我试图 print "@a\n";
但它只打印 ARRAY(0x9aa370)
这让我觉得推送没有做任何事情.任何帮助表示赞赏.谢谢!
我的@a = ();我的 $b = 10;我的 $c = 'a';推(@{$a[$b]}, $c);
让我们分解一下.
@{...}
理解为 perlref 中的使用引用"
在您将标识符(或标识符链)作为变量或子例程名称的一部分放置的任何地方,您都可以用返回正确类型引用的 BLOCK 替换标识符.
那么 { ... }
块中的内容最好用于数组引用.您在那里有 $a[$b]
,在索引 $b
处有一个 @a
元素,因此该元素必须是一个数组引用.
然后 @{...}
取消引用它并 push
es 一个新元素 $c
给它.总之,$c
被复制到一个匿名数组的(唯一)元素中,该元素的引用位于数组 @a
的索引 $b
.
还有一个关键部分:因为实际上没有 arrayref †,autovivification 启动并创建.由于 $b
之前的索引处没有元素,它们也被创建,值为 undef
.
现在请完成
教程 perlreftut 和
数据结构手册perldsc
同时使用 perlref
链接在开头以获得完整参考.
对于复杂的数据结构,能够看到它们很有用,并且有工具可以做到这一点.最常用的是核心Data::Dumper,这里有一个例子使用 Data::Dump>
perl -MData::Dump=dd -wE'@ary = (1);push @{$ary[3]}, "啊";dd \@ary'
带输出
<前>[1, undef, undef, ["啊"]]其中 []
里面表示一个 arrayref,它的唯一元素是字符串 ah
.
† 更准确地说,一个 undef
标量被取消引用,因为这发生在 lvalue 上下文中,自动激活就开始了.感谢 ikegami 的评论.例如,请参阅这篇文章及其链接.
Can anyone explain the push
statement in the following Perl code to me? I know how push in perl works but I can't understand what the first argument in following push command represents. I am trying to interpret someone's script. I tried to print "@a\n";
but it only printed ARRAY(0x9aa370)
which makes me think that the push is not doing anything. Any help is appreciated. Thanks!
my @a = ();
my $b = 10;
my $c = 'a';
push(@{$a[$b]}, $c);
Let's break it down.
The @{...}
is understood from "Using References" in perlref
Anywhere you'd put an identifier (or chain of identifiers) as part of a variable or subroutine name, you can replace the identifier with a BLOCK returning a reference of the correct type.
So what is inside { ... }
block had better work out to an array reference. You have $a[$b]
there, an element of @a
at index $b
, so that element must be an arrayref.
Then @{...}
dereferences it and push
es a new element $c
to it. Altogether, $c
is copied into a (sole) element of an anonymous array whose reference is at index $b
of the array @a
.
And a crucial part: as there is in fact no arrayref † there, the autovivification kicks in and it is created. Since there are no elements at indices preceding $b
they are created as well, with value undef
.
Now please work through
tutorial perlreftut and
data-structures cookbook perldsc
while using perlref
linked in the beginning for a full reference.
With complex data structures it is useful to be able to see them, and there are tools for that. A most often used one is the core Data::Dumper, and here is an example with Data::Dump
perl -MData::Dump=dd -wE'@ary = (1); push @{$ary[3]}, "ah"; dd \@ary'
with output
[1, undef, undef, ["ah"]]
where []
inside indicate an arrayref, with its sole element being the string ah
.
† More precisely, an undef
scalar is dereferenced and since this happens in an lvalue context the autovivification goes. Thanks to ikegami for a comment. See for instance this post with its links.
这篇关于Perl 中的引用是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!