Name: Anonymous 2005-07-30 4:54
I'm learning perl right now and I'm having a bit of a problem. I'm making a function that returns a list of the files (no directories) in a dir. It also has an option do do it recursively so the files in the subdirs also get in the list. The problem is that it won't work as it should when it's being recursive. It will only return the files of it's "first stop".
sub getdir {
my ($dirpath, $recurse) = @_;
my ($file, @dirlist);
if ($dirpath !~ /[\/\\]$/) {
$dirpath .= "/";
}
if (!opendir(DIR, $dirpath)) {
return 0;
}
while (defined($file = readdir(DIR))) {
next if $file =~ /^\.\.?$/;
$file = "$dirpath$file";
if ($recurse && -d $file) {
push (@dirlist, &getdir("$file/", 1));
}
elsif (! -d $file) {
push(@dirlist, "$file");
}
}
closedir(DIR);
return @dirlist;
}
sub getdir {
my ($dirpath, $recurse) = @_;
my ($file, @dirlist);
if ($dirpath !~ /[\/\\]$/) {
$dirpath .= "/";
}
if (!opendir(DIR, $dirpath)) {
return 0;
}
while (defined($file = readdir(DIR))) {
next if $file =~ /^\.\.?$/;
$file = "$dirpath$file";
if ($recurse && -d $file) {
push (@dirlist, &getdir("$file/", 1));
}
elsif (! -d $file) {
push(@dirlist, "$file");
}
}
closedir(DIR);
return @dirlist;
}