此示例代码中数组引用的范围重要吗
#!/usr/bin/perl
A();
B();
sub A {
my @array = qw(value_1 value_2);
$array_ref = \@array;
}
sub B {
foreach my $i ( @{$array_ref} ) {
print "Array Value: $i \n";
}
}
由于数组是使用“my”关键字声明的,数组引用是否会丢失? 任何人都可以向我介绍一下这一点吗?
#!/usr/bin/perl
A();
B();
sub A {
my @array = qw(value_1 value_2);
$array_ref = \@array;
}
sub B {
foreach my $i ( @{$array_ref} ) {
print "Array Value: $i \n";
}
}
Since the array is declared using the 'my' keyword, could the array reference be lost ?
Can any one brief me over this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,变量的作用域会过期,但内存地址不会过期。数据将保留。
这不是你可以简单尝试一下的吗? =)我只是复制/粘贴你的代码并尝试了一下,效果很好。
不过,为了正确封装,您实际上应该返回数组引用:
No, the scope of the variable expires, but not the memory address. The data will remain.
Isn't this something you could have simply tried? =) I just copy/pasted your code and tried it, and it worked fine.
For proper encapsulation, though, you really should return the array ref instead:
我绝对推荐
在 Perl 脚本中使用(把它放在最开始)。在这种情况下,它会抱怨 $array_ref 未声明为全局的。这可能是造成混乱的主要根源:您使用 $array_ref 而不以任何方式声明它,因此它被视为全局变量。
数组内容本身被保留,因为它被这个变量引用,所以引用计数保持大于 0(perl 在内部使用引用计数来跟踪何时删除变量)。
当然,建议使用 TLP 帖子(没有全局变量)中所示的方法。
I definitely recommend using
in Perl scripts (put it on the very beginning). In this very case it will complain about $array_ref being undeclared global. And it is likely the main source of the confusion: you use $array_ref without declaring it in any way, so it is treated as a global variable.
The array content itself is kept because it is referenced by this very variable so reference count remains greater than 0 (perl uses reference counting internally to keep track of when to remove variables).
Of course approach like shown in TLP posts (without globals) is to be recommended.
实际上,有一个很好的理由在此使用
my
示例。实际上,您希望每次通过子例程都重新创建变量,否则您将更改之前获得的值。
请注意
@array
的每个副本都是相同的。这就是为什么每次调用子例程时都需要一个新副本的原因。
Actually there is a very good reason to use
my
in this example.You actually want the variable to be re-created every time through the subroutine, otherwise you would change the values that you got earlier.
Notice how every copy of
@array
is identical.This is why you need a new copy for every time the subroutine is called.