Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have the current directory set to C: in the command prompt and am trying the simple code below with two different results.
The first line uses opendir dirs, '.' to get the files in the current directory and the second line uses opendir dirs2, 'C:\\' to do the same. I then compare them on the third line and get a return value of 1 telling me that they are the same.
In the while loop I loop through all the files and only print the ones that are tested true with -d $_. This works fine until I replace dirs with dirs2 and run the same code. Nothing is returned this time because -d $_ this time is returning a value of 0. If both dirs and dirs2 are the same why does the while loop work with one but not the other?
opendir dirs, '.' or die "Couldn't open current directory: $!"; opendir dirs2, 'C:\\' or die "Couldn't open current directory: $!"; print dirs == dirs2, "\n"; while ($_ = (readdir dirs)) { print $_, "\n"; if (-d $_) { print "This is a dir $_ \n"; } }
janitored by ybiC: Add balanced <readmore> tags and <p>aragraphs
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reading directories
by Beechbone (Friar) on Sep 23, 2003 at 13:21 UTC |