|
如果一个变量或子例程没有名字,那么它就是匿名的(anonymous)。如果把一个匿名变量(或子例程)复制给标量,那么这个标量就是对该变量(子例程)的引用。
匿名数组的元素放在方括号 ([ ]) 中。
测试代码:
[code=perl]#!/usr/bin/perl
my $arrayref = [ 'Tony', 'Jasmine', 'Gaby', 'Lazy' ];
#打印数组地址
print "The value of the reference, \$arrayref is ", $arrayref, "\n"; #
#各种引用方式
print "$arrayref->[3]", "\n";
print $arrayref[3], "\n";
print ${$arrayref}[3], "\n";
print "@{$arrayref}", "\n";
[/mw_shl_code]
运行输出:$ ./anonymous.pl
The value of the reference, $arrayref is ARRAY(0x8955830)
Lazy
Lazy
Lazy
Tony Jasmine Gaby Lazy
匿名散列
匿名散列使用花括号 ( { } ) 创建,如:
[code=perl]#!/usr/bin/perl
my $hashref = { "Name"=>"Tony",
"Type"=>"Student"
};
print $hashref->{"Name"}, "\n\n";
print keys %$hashref, "\n";
print values %$hashref, "\n";
[/mw_shl_code]
运行输出:$ ./anoyhash.pl
Tony
TypeName
StudentTony
也可以在散列中嵌套散列,在数组中嵌套散列,在数组中创建数组,从而构成复杂的数据结构。 |
|