日記

Perlのファイルグロブの不思議

Perlにはファイルグロブという機能がありファイル一覧を取得できます。通常はリストコンテキストで受け取るのですが、スカラーコンテキストだと例えばカレントディレクトリにa.txt, b.txt, c.txtと3つのファイルがある場合、次のコードの結果は

while($a=< *.txt>){
print "$a\n";
}

以下のようになります。

a.txt
b.txt
c.txt

となります。

しかし、次のようにループを展開すると常に最初のファイルが表示され、

$a=< *.txt>;
print "$a\n";
$a=< *.txt>;
print "$a\n";
$a=< *.txt>;
print "$a\n";

次のようになります。

a.txt
a.txt
a.txt

うーん、なんかすっきりしません。

similar posts

2 Comments

comment

よろしければ、コメントをどうぞ。トラックバックはこちら

このエントリーのコメントの購読

次のHTMLタグが使えます。: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

*Required Fields