Perl 引用操作全解析
1. 引用的使用与解引用
在 Perl 中,创建引用后,我们需要知道如何访问引用的数据。这个操作被称为解引用,规则很简单:在通常使用变量名的地方,将引用放在花括号内。
以数组为例,以下是解引用数组的示例代码:
#!/usr/bin/perl
# deref1.pl
use warnings;
use strict;
my @array = (2, 4, 6, 8, 10);
my $array_r = \@array;
print "This is our dereferenced array: @{$array_r}\n";
foreach (@{$array_r}) {
print "An element: $_\n";
}
print "The highest index is $#{$array_r}\n";
print "This is what our reference looks like: $array_r\n";
运行该程序,输出如下:
This is our dereferenced array: 2 4 6 8 10
An element: 2
An element: 4
An element: 6
An element: 8
An element: 10
The highest index is 4
This is what our reference looks like: ARRAY(0x806eb8)
从上述代码和输出可以看出,
超级会员免费看
订阅专栏 解锁全文
527

被折叠的 条评论
为什么被折叠?



