As jethro says, the next unless -d is an ugly hack that makes use of the fact that each time File::Find calls the supplied subroutine, $_ is set to the current file/directory, and cwd is changed to the parent of that file/dir.

To get a list of subdirectories below the current directory, you could write something like:

use File::Find; my @dir_list; sub wanted { if( -d $_ ) { push @dir_list, $File::Find::name; } } find( &wanted, '.' ); # @dir_list now contains the list of directories found.

However, seeing that once you have your list of directories, you plan to iterate over them, and insert files from them into your database, you could make your main program the wanted subroutine. That way you can simplify it as it only needs to consider one file at a time.

use File::Find; sub process_each_file { if( file_is_interesting($File::Find::name) ) { my $parsed_content = parse_file($File::Find::name); unless( content_is_boring($parsed_content) ) { my $sucess = insert_into_database($parsed_content); $log->error("problem") unless $sucess; } } } find( &process_each_file, '.' ); # All files have now been read & relevant stuff is in the database.

In reply to Re: recursive subdirectories by chrestomanci
in thread recursive subdirectories by Anonymous Monk

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.