in reply to Get newest folders inside directory
Is there any way to efficiently do this without having to use the opendir() function?Building upon BrowserUk's excellent solution, here is a way to include hidden directories whose name begins with the dot character (but it still excludes the special . and .. directories). Since this uses the File::Slurp CPAN module, I doubt it is as fast as using the built-in glob function (if that's what you mean by "efficient"). You can achieve the same functionality using glob, but the code would get a little ugly (read_dir might be a little cleaner looking).
use strict; use warnings; use File::Slurp qw(read_dir); my @newest = (sort { -M $a <=> -M $b } grep { -d } read_dir('./'))[0.. +19];
Another subtle difference is that read_dir will die if the directory can not be opened, whereas glob will not.
Why don't you want to use opendir?
|
|---|