Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

Comment on Listing directory contents not working (SUSE Linux issue?)...
Download Code
Re: Listing directory contents not working (SUSE Linux issue?)...
by choroba (Abbot) 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 );

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (8)
As of 2014-07-10 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (198 votes), past polls