inn*_*naM 24
使用-d文件检查操作符:
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
my $path = $ARGV[0];
die "Please specify which directory to search"
unless -d $path;
opendir( my $DIR, $path );
while ( my $entry = readdir $DIR ) {
next unless -d $path . '/' . $entry;
next if $entry eq '.' or $entry eq '..';
print "Found directory $entry\n";
}
closedir $DIR;
Run Code Online (Sandbox Code Playgroud)
如果您不需要遍历整个目录层次结构,File :: Slurp比File :: Find更容易使用.
use strict;
use warnings;
use File::Slurp qw( read_dir );
use File::Spec::Functions qw( catfile );
my $path = shift @ARGV;
my @sub_dirs = grep { -d } map { catfile $path, $_ } read_dir $path;
print $_, "\n" for @sub_dirs;
Run Code Online (Sandbox Code Playgroud)
如果您确实需要遍历层次结构,请检查CPAN是否有更友好的替代方案File::Find.
File :: Finder和File :: Find :: Rule是前端File::Find.
File :: Find :: Closures值得学习如何使用File::Find以及如何编写闭包.
File :: Next使用迭代器方法进行目录遍历并且看起来很有希望,尽管我从未使用它.
最后,根据TIMTOWTDI的精神,这里有一些快速和肮脏的东西:
my @sub_dirs = grep {-d} glob("$ARGV[0]/*");
Run Code Online (Sandbox Code Playgroud)