in reply to glob() and dot files
Nowadays, Perl glob is a lot more uniform and well behaved. This prints all simple files, but skips directories.
For what you want, I would consider File::Find.my @files = glob ('*.*'); print "",join("\n",@files),"\n"
I think in Unix there can be special things that are not simple files or directories. I would use a file test to see what this name actually means.use strict; use warnings; opendir (my $dir, ".") or die "unable to open current directory $_"; my @files; my @directories; foreach my $file (grep{ ($_ ne '.') and ($_ ne '..')} readdir $dir) { if (-f $file ) {push @files, $file;} elsif (-d $file) {push @directories, $file;} else { die "weird beast found! $file"} } print "@files\n"; print "@directories\n";
Update: File operations like "open file" or "open directory" are "expensive" in terms of performance. I would expect my code to run faster than the OP's code, but I did not benchmark this in any serious way. If the directories are small and this is not done that often, I don't think that will make any difference at all. Also be aware that there is a special variable for repeated file tests, "_". like elsif (-d _) {do something{ That tests the structure returned by the previous file test operation for a different flag.
Overall, unless there is a performance or other problem (special kinds of files), I see no problem with the OP's code.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: glob() and dot files
by haukex (Archbishop) on Apr 13, 2020 at 08:26 UTC | |
by Marshall (Canon) on Apr 13, 2020 at 09:04 UTC | |
by haukex (Archbishop) on Apr 16, 2020 at 20:17 UTC | |
by Marshall (Canon) on Apr 18, 2020 at 06:38 UTC |