関数順 インデックス
目的別 インデックス

構文
 
 closedir ディレクトリ・ハンドル

返り値
 
 ディレクトリのクローズに成功した場合に真

説明

  • opendirでオープンしたディレクトリ・ハンドルをクローズします。

使用例

ディレクトリ/home/user1の中の一覧を表示する
#!/usr/bin/perl
use strict;
use warnings;

my $dirname = '/home/user1';
opendir my $dh, $dirname or die "$!:$dirname";
while (my $dir = readdir $dh) {
  print "$dir\n";
}
closedir $dh;
ディレクトリ/home/user1の中のファイルの一覧を表示する
#!/usr/bin/perl
use strict;
use warnings;

my $dirname = '/home/user1';
opendir my $dh, $dirname or die "$!:$dirname";
while (my $dir = readdir $dh) {
  my $fullpath = "$dirname/$dir";
  next unless -f $fullpath;
  print "$fullpath\n";
}
closedir $dh;
※ファイルだけの一覧が欲しい場合はファイル・テスト演算子の-fを使います。