ディレクトリエントリの読み方

Windowsでは、ディレクトリ名にスペースを許容するため、glob関数を使うと困ったことになります。

use strict;
use warnings;
use Cwd;

print join("\n", glob(getcwd)), "\n";

マイドキュメントの上で実行した場合の結果は以下の通り。glob関数は複数のパターンを扱えるためです。

C:/Documents
and
Settings/ユーザ名.サーバ名/My
Documents/perl

なので、readdir関数を使うことが多いです。DirHandleモジュールを使うとちょっと便利。5.005辺りでも標準モジュールなので、安心して使えます。

use strict;
use warnings;
use Cwd;
use DirHandle;

my $dh = DirHandle->new(getcwd) or die $!;
print join("\n", $dh->read), "\n";
$dh->close;

使い捨てのスクリプトなら、File::Slurpモジュールを使ってしまう方法も。

use strict;
use warnings;
use Cwd;
use File::Slurp;

print join("\n", read_dir(getcwd)), "\n";

File::Slurpでは"."と".."が入りません。