您发布的所有三种变体都会在use strict
之下产生语法错误,并且在Perls 5.22之前的Perl上会出现use warnings
的额外警告。我在这里展示的输出来自Perl 5.20.1。
use strict;
use warnings;
my $par = { a => { b => 1, c => 2, d => 3 } };
my $sc1 = %par->{'a'}{'b'};
my @a1 = %par->{'a'}{'c'};
my %c3 = %par->{'a'}{'d'};
__END__
Using a hash as a reference is deprecated at /home/foo/code/scratch.pl line 700.
Using a hash as a reference is deprecated at /home/foo/code/scratch.pl line 701.
Using a hash as a reference is deprecated at /home/foo/code/scratch.pl line 702.
Global symbol "%par" requires explicit package name at /home/foo/code/scratch.pl line 700.
Global symbol "%par" requires explicit package name at /home/foo/code/scratch.pl line 701.
Global symbol "%par" requires explicit package name at /home/foo/code/scratch.pl line 702.
Execution of /home/foo/code/scratch.pl aborted due to compilation errors.
没有strict
和warnings
,它会编译,但产生的废话。
no strict;
no warnings;
use Data::Printer;
my $par = { a => { b => 1, c => 2, d => 3 } };
my $sc1 = %par->{'a'}{'b'};
my @a1 = %par->{'a'}{'c'};
my %c3 = %par->{'a'}{'d'};
p $sc1;
p @a1;
p %c3;
__END__
undef
[
[0] undef
]
{
'' undef
}
这就是说,永远为你的Perl程序use strict
和use warnings
,听它表明你的警告。
它意味着一个错误。把它扔掉。 –