in reply to recursive subdirectories

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.

Replies are listed 'Best First'.
Re^2: recursive subdirectories
by Anonymous Monk on Jan 28, 2011 at 15:53 UTC
    Thanks for being so helpful. How do i get it to NOT include the current working directory? </code> sub process_each_file { if( -d $File::Find::name) { print $File::Find::name, "\n"; } } find( \&process_each_file, '.' ); </code> The first item in the list is .