在 perl 中,项目将在检查时自动激活,不需要分配。到达最里面的请求键所需的项目路径将在检查时创建。请注意,{d => undef} is 条目实际上并未创建,而是隐含的。
use strict;
use warnings;
use Data::Dumper;
my %a; # as is empty, equivalent to \%a is {};
print Dumper %a;
$a{b}{c}{d}; # \%a is now { b => { c => {}}}
# returns an undef value.
print Dumper \%a;
输出:
$VAR1 = {};
$VAR1 = {
'b' => {
'c' => {}
}
};
perl 数组示例:
use strict;
use warnings;
use Data::Dumper;
my (@b,@c); # @b=(), @c=()
print Dumper \@b;
$b[3]; # @b=() aka unchanged.
print Dumper \@b;
$b[3][2][1]; # @b=(undef,undef,undef,[undef,undef,[]])
print Dumper \@b;
print Dumper \@c;
$c[3]=1 ; # @c=(undef,undef,undef,1)
print Dumper \@c;
输出:
Useless use of array element in void context at -e line 7.
Useless use of array element in void context at -e line 9.
$VAR1 = [];
$VAR1 = [];
$VAR1 = [
undef,
undef,
undef,
[
undef,
undef,
[]
]
];
$VAR1 = [];
$VAR1 = [
undef,
undef,
undef,
1
];