在C里,间接的最常见的形式就是指针,它可以让一个变量保存另一个变量的内存地址。在Perl里,间接的最常见的形式是引用。
8.2.1 用反斜杠创建引用:
\ 这个操作符的作用类似于C里的&(取址)操作符
下面是一些例子:
$scalarref = \$foo;
$constref = \186_282.42;
$arrayref = \@ARGV;
$hashref = \%ENV;
$coderef = \&handler;
$globref = \*STDOUT;
反斜杠操作符可以做的事情远远不止生成一个引用。如果你对一个列表使用反斜杠,那么它会生成 一整列引用。参
阅“你用硬引用可以实现的其他技巧”一节。
简单变量的引用和解引用:
[oracle@jhoa 8]$ cat 2.pl
$foo = "abc";
$scalarref = \$foo;
print "\$scalarref is $scalarref \n";
print "\$scalarref is $$scalarref \n";
$constref = \'186_282.42';
print "\$constref is $constref\n";
print "\$constref is $$constref\n";
[oracle@jhoa 8]$ perl 2.pl
$scalarref is SCALAR(0x12706080)
$scalarref is abc
$constref is SCALAR(0x12728f10)
$constref is 186_282.42
数组的引用:
[oracle@jhoa 8]$ cat 3.pl
@ARGV=qw/a b c d e f g/;
$arrayref = \@ARGV;
print "\$arrayref is $arrayref\n";
prin
第八章 引用
最新推荐文章于 2018-05-21 14:57:36 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)