Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Listing directory contents not working (SUSE Linux issue?)...

by aquinom (Acolyte)
on Apr 09, 2013 at 19:20 UTC ( #1027820=perlquestion: print w/replies, xml ) Need Help??
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 (Bishop) 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") {
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        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 );

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1027820]
Front-paged by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2018-06-20 21:30 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.