Zenshai has asked for the wisdom of the Perl Monks concerning the following question:
There is a bug somewhere in there, that causes the wanted function to add a top level 'home' (or other) directory to the array instead of the 'home' directory's subdirs. This doesn't happen every time, and I have not been able to discern a pattern for why this happens, other than that it always follows a 'subtracting from depth' debug print statement.# this subroutine will take path and use File::Find to decend # one directory level deeper and list the directories it finds there # if it encounters a 'home', 'shared', or 'expansions' directory it'll # descend [even] one level deeper than otherwise sub go_deeper { #arguments 1) unc style path string i.e. '\\fileshare1\froot$' #returns 1) array with paths to directories below the given path # find directories my $path_line = shift; chomp($path_line); # delete trailing backslash if its there if ( substr( $path_line, -1 ) eq '\\' ) { substr( $path_line, -1 ) = ''; } our $depth = 0; our $original_depth = $depth; our @dirs_list = (); find( { preprocess => \&preprocess, wanted => \&wanted }, $path_line ); sub wanted { my $dir = $File::Find::name; print LOG "DEBUG Entered wanted function. CWD: $dir \n"; if ( ( index( lc($dir), 'home' ) != -1 || index( lc($dir), 'expansion' ) != -1 || index( lc($dir), 'share' ) != -1 ) and $depth == $original_depth ) { print LOG "DEBUG adding to depth\n"; $depth++; } elsif ( $depth != $original_depth ) { print LOG "DEBUG subtracting from depth\n"; $depth--; } my $depth_count = $File::Find::dir =~ tr[/][]; # count slashes to get depth return if $depth_count < $depth; if ( (-d) && ( $_ ne '.' ) ) { $dir =~ s/\//\\/g; print LOG "DEBUG Adding dir to file: " . $dir . "\n"; push( @dirs_list, $dir ); } } sub preprocess { my $depth_count = $File::Find::dir =~ tr[/][]; return @_ if $depth < $depth; if ( $depth_count == $depth ) { return grep { -d } @_; } return; } return @dirs_list; }
DEBUG Adding dir to file: \\fileshare2\groot$\shared\EnterpriseD DEBUG Entered wanted function. CWD: \\fileshare2\groot$/shared/MIG020907 DEBUG subtracting from depth <=== THIS WAS LAST DIR AT THIS DEPTH, SO ITS BACKING OUT DEBUG Adding dir to file: \\fileshare2\groot$\shared\MIG020907 DEBUG Entered wanted function. CWD: \\fileshare2\groot$/shared/ITReg DEBUG adding to depth DEBUG Adding dir to file: \\fileshare2\groot$\shared\ITReg DEBUG Entered wanted function. CWD: \\fileshare2\groot$/home DEBUG subtracting from depth DEBUG Adding dir to file: \\fileshare2\groot$\home <=== THIS LINE SHOULD NOT BE ADDED DEBUG Entered wanted function. CWD: \\fileshare2\groot$/EXPANSIONS DEBUG adding to depth DEBUG Entered wanted function. CWD: \\fileshare2\groot$/EXPANSIONS/old DEBUG subtracting from depth DEBUG Adding dir to file: \\fileshare2\groot$\EXPANSIONS\oldMy instincts are telling me this has something to do with this dir being the last one in the list or something similar, linked to the recursive nature of file::find, but nothing I've tried has yielded results. Please help me out.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Hard to find bug in my File::Find code
by kyle (Abbot) on Aug 29, 2008 at 18:22 UTC | |
by Zenshai (Sexton) on Aug 29, 2008 at 19:43 UTC | |
|
Re: Hard to find bug in my File::Find code
by tilly (Archbishop) on Aug 29, 2008 at 18:23 UTC | |
by Zenshai (Sexton) on Aug 29, 2008 at 20:03 UTC | |
|
Re: Hard to find bug in my File::Find code
by runrig (Abbot) on Aug 29, 2008 at 18:50 UTC | |
|
Re: Hard to find bug in my File::Find code
by jethro (Monsignor) on Aug 29, 2008 at 18:14 UTC |