http://www.perlmonks.org?node_id=171767

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am still persuing a solution to what should be a trivial problem:

How to get a list of the immediate subdirs of a user-supplied path:

I have a solution (thanks to the help of the wanderers of these noble halls) that works for the supplied path of "." but NOT for a supplied relative or absolute path UNLESS that is the cwd!?

Is this a code (dumb user) error? A platform (dumb os) specific error? Or -- horror of horrors -- the result of a Perl library writers off-day?

The following code is used to demonstrate the problem:

C:\www\NB.biz\html>type test.pl #!e:/perl/bin/perl.exe -w use strict; use diagnostics; print "Arg:", $ARGV[0], "\n"; $, = "\n"; opendir(DIR, $ARGV[0] ) or die ".: $!"; print "\nReaddir:\n", readdir(DIR); rewinddir(DIR); print "\nOnly directories:\n", grep {-d } readdir(DIR); rewinddir(DIR); print "\nJust named directories:\n", grep { -d $_ and $_ ne '.' and $ +_ ne '..'} read dir(DIR); closedir(DIR);

I pushed the long demonstration of the code failing here:

C:\www\NB.biz\html>test.pl . Arg:. Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. css graphics Products Just named directories: css graphics Products C:\www\NB.biz\html>cd .. C:\www\NB.biz>html\test.pl html Arg:html Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. Just named directories: C:\www\NB.biz>html\test.pl html\ Arg:html\ Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. Just named directories: C:\www\NB.biz>html\test.pl html\. Arg:html\. Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. Just named directories: C:\www\NB.biz>html\test.pl \www\NB.biz\html Arg:\www\NB.biz\html Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. Just named directories: C:\www\NB.biz>html\test.pl \www\NB.biz\html\ Arg:\www\NB.biz\html\ Readdir: . .. blank.htm css Document graphics index.htm logo.htm Menu.pl NB.biz.tws Products test.pl Only directories: . .. Just named directories: C:\www\NB.biz>