in reply to Quickest way to get a list of all folders in a directory

You also have to test that what you're getting are really dirs.

You misread the 'perldoc -f readdir', what they mean by next directory entry is not that the entry is a directory- but that the element is an entry in the directory you opened.

I made my very silly directory subs.. may be of use.. LEOCHARRE::Dir. But really it's best to learn to do this.

Note that topkela made an excellent suggestion.

  1. Isolate your directory path value my $directory = "//pcname-vm/c$/program files/adobe";
  2. Test that the value makes sense, that it's a dir, that it can be opened..
    -d $directory or die("Not dir: '$directory'"); opendir(DIR, $directory) or die("cant open '$directory', $!");
  3. Proceed to read the directory entries..
(updated die(s) as ikegami pointed out)

Replies are listed 'Best First'.
Re^2: Quickest way to get a list of all folders in a directory
by ikegami (Patriarch) on Aug 13, 2009 at 15:03 UTC

    You also have to test that what you're getting are really dirs.

    No. opendir already does that. Checking it explicitly is redundant at best, and adds a race condition at worse.

    Isolate your directory path value

    That's a great idea since it allows you to print it in the error message, something you failed to do. This would have been extremely useful to the OP.

    $ perl -e' my ($dir_qn) = @ARGV; opendir(my $dh, $dir_qn) or die "Can'\''t open dir $dir_qn: $!\n"; ' somefile Can't open dir somefile: Not a directory

      Yes- I was thinking here about the person reading the code more than the code- it would make it extra special with sugar on top obvious about what the problem is or is not.. And.. if the OP is not even checking that the entries are files or dirs or what- then seeing a line that tells -d or x.. might be needed? For the human, that is.

      But you're right, very good point, opendir() already checks that it's a dir.

        I was thinking here about the person reading the code more than the code

        Maybe it's just me, but I think the person also doesn't need a -d check in addition to opendir to know a directory is being opened.