大域変数を使用禁止にする

プラグマuse strict;を指定すると,大域変数の使用を禁止することができます.まず,このプログラムは動きます.

@bar=(1,2,3);
print "@bar\n";

#実行結果
bash-2.05$ perl sam.pl
1 2 3
  

次のプログラムは動きません.配列変数@barが大域変数であるためです.

use strict;

@bar=(1,2,3); #大域変数@bar
print "@bar\n";

#実行結果
bash-2.05$ perl sam2.pl
Global symbol "@bar" requires explicit package name at sam2.pl line 3.
Global symbol "@bar" requires explicit package name at sam2.pl line 4.
Execution of sam2.pl aborted due to compilation errors.
  

配列変数@barを局所変数にすると動きます.

use strict;

my @bar=(1,2,3); #局所変数@bar
print "@bar\n";

#実行結果
bash-2.05$ perl sam3.pl
1 2 3
  

動作環境は,WindowsNT Workstation 4.0+SP6,cygwin 1.3.3,perl 5.6.1,bash 2.05です.