構文 |
|
opendir ディレクトリ・ハンドル, ディレクトリ名 |
返り値 |
|
・ディレクトリのオープンに成功した場合に真 |
説明
- ディレクトリ名という名前のディレクトリをオープンしてディレクトリ・ハンドルに結びつけます。readdir関数にディレクトリ・ハンドルを渡すことによってディレクトリのエントリを取得することができます。
使用例
#!/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;
#!/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;
#!/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 $fullpath =~ m/\.csv$/i;
next unless -f $fullpath;
print "$fullpath\n";
}
closedir $dh;
#!/usr/bin/perl
use strict;
use warnings;
my $search = '/home/user1/*.csv';
while (my $file = glob($search)) {
print $file, "\n";
}