You're looping on the first item in the list which will
be a '.'
Works better if you add
next if $item eq '.' or $item eq '..'; after
the foreach
readdir gives you the names in the directory...
not path names, so when you call the printdir recursively
you'll need to prefix the current directory to each element
of the list. (Naturally you'll need to change your '.' test
accordingly.
Anyway this seems to work on my machine.
#!perl -w
use strict;
use Carp;
printdir(@ARGV);
sub printdir {
my $item;
foreach $item(@_) {
next if $item =~ /\.{1,2}$/;
if (-d $item) {
print "$item\n";
opendir(SUBDIR, $item) or croak "Can't open directory :$!";
my @subdir_items = readdir(SUBDIR); #+
closedir(SUBDIR); #+
printdir(map {"$item/$_"}@subdir_items); #+
}
}
}
Oh, by the way the equivalent in my previous post should have been
perl -Mstrict -MFile::Find -wle 'find sub{print $File::Find::dir if -d},@ARGV' .
but you saw that already, didn't you :)
Update: Just saw merlyn's post. The above ran on
Win32 because (as he says) the Windows port recognizes forward slashes
as valid directory separators. I don't know whether this
is an issue on other non unix ports.
Still, I agree with him, it's a lot safer here to stay
with the standard.
Update 2: The '.' test is broken: it will match
any file ending with a '.'. You'd need to split off the
file name from the path, so you might as well use
File::Spec... Oh good grief, why did I ever post this code?
This'll teach me to shut up :(
|