Re: recurse subdirectories without using File::Find
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 1 Aug 2006 09:02:56 -0700
jedmallen@xxxxxxxxx wrote:
Subject: recurse subdirectories without using File::Find
Why? Why are you ignoring a known non-buggy solution in favor of
writing your own, admittedly buggy solution?
if this script is fed with a small directory tree, it works ok. but if
the argument is a large directory tree, when it encounters a directory,
it will dig deep into it, but it never comes back up to finish the
other dirs in the parent dir.
What does that mean? Can you please produce a short-but-complete
example that demonstrates the problem you're seeing? Show your sample
input (which is a directory structure in this case) and output.
how do i do it?
You start by writing better code, and asking Perl for help whenever you
can.
thanks!
!-- snip --!
#!/usr/bin/perl -w
You forgot:
use strict;
&dirlist($ARGV[0]);
Why are you calling the subroutine with the & on front? Do you know
the two side effects that causes? Do you want those side effects?
Probably not.
sub dirlist {
local $dir = $_[0];
Why are you making this local? The effect of that would be to make
$dir have the same value in any called subroutines. But the only
subroutine you're calling is this one - in which $dir immediately gets
set to a different value.
opendir(DH, $dir);
Why aren't you checking the return value of this call? You have no way
of knowing that $dir was actually opened.
Additionally, you want to be using lexical directory handles, not
global barewords.
opendir my $DH, $dir or die "Cannot open '$dir': $!";
@files = readdir(DH);
closedir(DH);
foreach $file(@files) {
So you're putting all the files into memory, only to loop through them
one at a time? Why? Why not just read one, process it, and discard
it?
while (my $file = readdir $DH) {
next if ( $file =~ /^\./ );
$full_path = $dir . "/" . $file;
if (-f $full_path) {
print $full_path . "\n";
} else {
&dirlist($full_path);
}
}
} # sub dirlist ends
Please post a full complete example of how your - now corrected - code
is malfunctioning.
Paul Lalli
.
- References:
- recurse subdirectories without using File::Find
- From: jedmallen@xxxxxxxxx
- recurse subdirectories without using File::Find
- Prev by Date: Re: if-clause again
- Next by Date: Is this behavior not right?
- Previous by thread: recurse subdirectories without using File::Find
- Next by thread: Is this behavior not right?
- Index(es):
Relevant Pages
|