opendir(DIR, "/home/a") || die "can't open dir: $!"; my $count = grep(/\.txt$/, readdir(DIR)); closedir(DIR); print "There are $count .txt files\n";