Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
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 chilling in the Monastery: (3)
As of 2014-09-20 01:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls