in reply to Return an Array of Sub-Dir Names
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; }
|
|---|