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

Hi all,

I wrote a basic directory parser and my system refuses to output the contents using the -d check, although without using it everything gets output. So I'm sort of at a loss. I'll post the code, just so you can see I'm not crazy.

My question is, does anyone have any idea what would be causing this? ls -d does the same thing, it only reports that ". and .." are directories.

edit: actually my last observation that I added (without thinking about what I was writing) clearly demonstrates that this is a system thing and nothing to do with Perl. Best.

my $dir = "/"; opendir(DH, $dir) or die "Couldn't open directory: $dir"; while(my $line = readdir(DH)){ chomp $line; if ($line =~ /^\./){ next; } elsif (-d $line){ print "$line\n"; } } close(DH);

Replies are listed 'Best First'.
Re: Listing directory contents not working (SUSE Linux issue?)...
by choroba (Cardinal) on Apr 09, 2013 at 19:45 UTC
    readdir returns just the directory name, not the full path. If you are not running the script from /, your -d test looks for directories in the directory you started the script from. Prepend $dir to the directory name in the test to make it work:
    elsif (-d "$dir$line") {
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks.

        Actually, you don't need to "chomp" the values returned by readdir.

Re: Listing directory contents not working (SUSE Linux issue?)...
by Anonymous Monk on Apr 10, 2013 at 07:58 UTC

    use File::Find::Rule;
    my @dirs = find( directory => maxdepth => 1, in => $dir );