in reply to glob() and dot files
You might be interested in my node To glob or not to glob for the caveats of glob.
What would be the easiest to accomplish "listing all files/subdirectories including dotfiles/dotsubdirs but without the . and .." and "listing all dotfiles/dotsubdirs only, without the . and .."?
If you mean core-only, then:
use File::Spec::Functions qw/ no_upwards catfile catdir /; opendir my $dh, $path or die "$path: $!"; my @files = map { -d catdir($path,$_) ? catdir($path,$_) : catfile($path,$_) } sort +no_upwards readdir $dh; closedir $dh;
Note: I think that on some OSes (VMS?), there's a difference between catfile and catdir, that would require you to use the -d test, but I believe the above should work fine on any other OS. (Or, you can omit the catfile entirely if bare filenames are ok.) <update> Confirmed the difference between catfile and catdir with File::Spec::VMS and File::Spec::Mac, so I updated the above example with the -d test accordingly. </update> <update2> I don't have a VMS or Classic Mac to test on, but I realized that my update had a bug in that I wasn't doing the -d test on the full filename. So I hope that this updated version would really be correct on those platforms. </update2>
If you need absolute pathnames, you probably want to add a $path = rel2abs($path); (also from File::Spec). Otherwise, if CPAN is fine, then I really like Path::Class, its children includes everything except the . and .. by default:
use Path::Class; my @files = dir($path)->children(); # - or - my @files = dir($path)->absolute->children();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: glob() and dot files (updated)
by perlancar (Hermit) on Apr 13, 2020 at 09:46 UTC | |
by haukex (Archbishop) on Apr 13, 2020 at 09:59 UTC | |
by soonix (Chancellor) on Apr 13, 2020 at 11:28 UTC |