How can I list all files in a directory using Perl? -


i use like

my $dir="/path/to/dir"; opendir(dir, $dir) or die "can't open $dir: $!"; @files = readdir dir; closedir dir; 

or use glob, anyway, need add line or 2 filter out . , .. quite annoying. how go common task?

i use glob method:

for $file (glob "$dir/*") {     #do stuff $file } 

this works fine unless directory has lots of files in it. in cases have switch readdir in while loop (putting readdir in list context bad glob):

open $dh, $dir     or die "could not open $dir: $!";  while (my $file = readdir $dh) {     next if $file =~ /^[.]/;     #do stuff $file } 

often though, if reading bunch of files in directory, want read them in recursive manner. in cases use file::find:

use file::find;  find sub {     return if /^[.]/;     #do stuff $_ or $file::find::name }, $dir; 

Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -