ロンドンの「せっかちなperl」を読んでいます。「リファレンス」の章の例をテストしています。参照の自動有効化で [] に数字 (任意の数字) を入れる必要があるのに、配列を宣言するときは [] を空の配列として使用できるのはなぜだろうと思っています。ありがとう。
#!/usr/bin/env perl
use warnings;
use strict;
use Data::Dumper;
my $scal;
my $val = $scal->[2]->{somekey}->[1]->{otherkey}->[7];
# fails if [] instead of [7] or [1] or [99999];
# same result if [7] or [1] or [99999] is used;
$val->[3] = 19;
print Dumper $scal;
print "========\n";
print Dumper $val;
print "========\n";
print Dumper []; # this does not fail;
エラー メッセージは、「"[]" 付近の referenceTest.pl 行 7 で構文エラーが発生しました。グローバル シンボル "$val" には、referenceTest.pl 行 15 で明示的なパッケージ名が必要です。コンパイル エラーのため、referenceTest.pl の実行が中止されました。」
================== [7] を使用して動作する場合、結果は次のようになります。
$VAR1 = [
undef,
undef,
{
'somekey' => [
undef,
{
'otherkey' => []
}
]
}
];
========
$VAR1 = [
undef,
undef,
undef,
19
];
========
$VAR1 = [];
教えてくれてありがとう。