Re: recurse subdirectories without using File::Find



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

.



Relevant Pages

  • Re: writing drivers using C++
    ... You are trying to sidestep the issue by writing about something ... coding against an interface - when the implementation starts mattering to ... Every subroutine invokation has to be coded manually, ...
    (comp.os.linux.development.system)
  • Re: [QUIZ][SOLUTION] Kalah (#58)
    ... preceding URL makes it look harder and more cumbersome than just writing a ... subroutine to exercise your new class. ...
    (comp.lang.ruby)
  • Re: solving an equation multiple directions
    ... On Fri, 16 Jan 2009, Jason wrote: ... writing the equation twice. ... but without writing two equations to prevent duplication in my code. ... Perhaps you want a subroutine. ...
    (sci.math)
  • Re: use go to doing some case studies and induced problems
    ... these case studies are not so worthy of writing them as ... If for some reason I really didn't want a separate routine with an argument list, I'd use an internal subroutine. ... Anything so short that it's not worth making a subroutine for, I'd put inline in the code. ... I'd have to say, this sounds like it could easily lead to GIGO (garbage in, garbage out) when you miss something in the logic. ...
    (comp.lang.fortran)