in reply to Return an Array of Sub-Dir Names

This subroutine appears to be written for perl 4 (update: which is over 7 years out of date.) . This is evidenced by the use of local, not my which wasn't in perl 4, and the lack of grep or push which also weren't in perl 4 (strikethrough is update as of 27 Jan 2002, thanks to grinder). In perl 5, my is strongly preferred to local in most circumstances (see also perlsub under the heading "Temporary Values via local()"). Also grep is much easier to use then a for loop for "filtering" a list.

This subroutine also relies on . and .. being returned from readdir first. This is not a portable assumption.

Here is the subroutine re-written not to have those problems:

sub get_sub_dirs { use File::Spec; # so we don't have problems with # platforms which use odd path # seperators. my($path) = @_; my @dirs; opendir(DIR, $path) or die "Cannot open $path: $!\n"; @dirs = grep { # not . or .. !/\A\.{2}\z/ # XXX: Won't work on OSes where # normal directories can be called # '.' or '..' (e.g. MacOS)? and # is a directory -d File::Spec->catfile($path, $_); # (using catfile no chdir is needed; # we just get the full pathname of # the file in question.) } readdir DIR; closedir DIR; return @dirs; }