Tip #1 from the Basic debugging checklist: warnings
readdir() attempted on invalid dirhandle DIR at closedir() attempted on invalid dirhandle DIR at
Using lexical filehandles seems to work for me:
use warnings; use strict; use Cwd; ## cwd my $cwd = getcwd; ## Get files from cwd my $res = _list_files($cwd); sub _list_files { my $dir = shift; my $files = shift; opendir(my $dh, $dir) || die "Error opening: $dir\n"; while (my $fn = readdir($dh)) { ## skip hidden next if $fn =~ /^\./; ## file, fullpath name my $file = $dir . "/" . $fn; ## add push(@{ $files }, $file); ## dir, decend if (-d $file) { ## sub-directory files $files = _list_files($file, $files); } } closedir($dh); return $files; }
See also:
In reply to Re: Descending a directory tree, returning a list of files
by toolic
in thread Descending a directory tree, returning a list of files
by Rodster001
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |