note
kyle
<p>I haven't <em>benchmarked</em> this (with [mod://Benchmark]) or anything, but a quick [doc://opendir]/[doc://readdir] would get it done:
<c>
my $directory_name = shift || '.';
opendir my $dh, $directory_name
or die "Can't opendir '$directory_name': $!";
my $file_count = scalar grep { -f "$directory_name/$_" } readdir $dh;
closedir $dh or die "Can't closedir: $!";
</c>
<p>It's bigger and more complicated than a [doc://glob]-based solution, but you get nice error messages in case there's problems.
<p>If you're interested in all directory <em>entries</em> (i.e., subdirectories and not just files), you can make the [doc://grep] match <c>{ !m{ \A \.\.? \z}x }</c> instead (so it still skips "." and "..").
<p><strong>Updated</STRONG> to fix a problem helpfully pointed out by [Corion]. The <c>-f</c> needs to have the directory named explicitly in case it's not the current directory.
606763
606763