您發佈的所有三種變體都會在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
,聽它表明你的警告。
它意味着一個錯誤。把它扔掉。 –