Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

-d misses some directories ?

by ady (Deacon)
on Aug 27, 2006 at 18:28 UTC ( [id://569889]=perlquestion: print w/replies, xml ) Need Help??

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

Reverend guys; -- The following code :
use strict; use warnings; use Data::Dumper; my $path = shift @ARGV; opendir (DIR, $path) or warn "Couldn't open $path: $!"; my @dirs = sort readdir(DIR); closedir(DIR); print "$path\n\t", Dumper(\@dirs);

Will print (for instance) :
C:\tmp>test.pl "C:\Documents and Settings\z6and\Desktop\x" C:\Documents and Settings\z6and\Desktop\x $VAR1 = [ '.', '..', '328-24.08.2006-14.30', '335-25.08.2006-06.40' ];
in a directory containing two subdirectories (besides the current & parent dirs; -- this is as expected).

But when i change line 7 to my @dirs = sort grep { -d } readdir(DIR); i get :
C:\tmp>test.pl "C:\Documents and Settings\z6and\Desktop\x" C:\Documents and Settings\z6and\Desktop\x $VAR1 = [ '.', '..' ];
which is NOT what i expect, as all four entries in the above listing are indeed directories...

Any explanation of this behaviour ?
Best regards/
Allan

Replies are listed 'Best First'.
Re: -d misses some directories ?
by Corion (Patriarch) on Aug 27, 2006 at 18:32 UTC

    Most likely, the current directory is not what you seem to think it is.

    -d works as designed, but it interprets the argument you give to it as a relative directory name unless it starts with a device letter or directory separator. Maybe you wanted grep { -d "$path/$_" } instead?

      aHA! Yes indeed, that twist makes it shout, as expected. Thanks Corion!
      / allan

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://569889]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-20 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found