Hello Monks,

I have the following sub which works as intended about 90% of the time.
# 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; }
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.

Here's what it looks like in the log file:
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\old
My 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.

In reply to Hard to find bug in my File::Find code by Zenshai

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.