perlのスコープはブロック or ファイルなのだろうか
ふとした拍子につまづいた。
以下を1個のファイル、例えば t.pl にでも書いて実行する。
package Hoge; use strict; use warnings; my $h = 1; package main; use strict; use warnings; my $h = 2; warn $h;
さあ実行結果を予測しよう。
実行結果
% perl t.pl "my" variable $h masks earlier declaration in same scope at t.pl line 9. 2 at t.pl line 10.
何でやねん。
そしてmainの中でのmy宣言を抜かすと
1 at t.pl line 9.
となる。