Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I'm trying to move through the subdirectories in the current directory and struggling. I have found this code

find ( sub { next unless -d; print $File::Find::name, "\n"; }, '.' );
which i don't fully understand but it prints the subdirectories.

What is the line 'next unless -d' doing?

How do i get this to return a list of the directory names so that i can then do something with the files in that directory?

Or perhaps i need to do something different altogether?The overall function i am trying to achieve is to go through each subdirectory in my current directory and then load the files in that directory into various databases. I was trying to get a list of subdirectories and was then going to loop through them

thanks a lot

Replies are listed 'Best First'.
Re: recursive subdirectories
by jethro (Monsignor) on Jan 28, 2011 at 15:20 UTC

    You might want to read the documentation of File::Find. It is a very useful module to traverse a directory structure. Basically you provide a subroutine and File::Find calls that subroutine for every file that it finds in a directory

    Also it sets $_ to the path of every file. So in your example code "-d" tests if the filename in $_ is a directory. If not, the subroutine is exited prematurely through a hack. It really should be "return unless -d;". I suspect "next" works only because someone found out that in the File::Find module that calls your subroutine there is a loop which then starts the next iteration

    Naturally you can use the subroutine to collect all files in a global array. But if you just want to do the same thing to all the files, do that something inside this subroutine and ignore all directories, i.e. use "return if -d;" instead.

Re: recursive subdirectories
by chrestomanci (Priest) on Jan 28, 2011 at 15:36 UTC

    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.
      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 .
Re: recursive subdirectories
by Anonymous Monk on Jan 28, 2011 at 15:10 UTC
    I am guessing the find method passes into the subroutine everything found in the path that is the second parameter which in this case is the current working direcoty. The first line of the subroutine is 'next unless -d' but how can you have 'next' in a subroutine. I've only seen it in a loop. It seems to exit the subroutine. Is this an ok thing to do?
Re: recursive subdirectories
by locked_user sundialsvc4 (Abbot) on Jan 28, 2011 at 17:42 UTC

    The -d bit is actually a “file test” operator.   The documentation on this is a bit hard to find, but here's a way:   perldoc -f “-d”

    There are a lot of “file finding” packages out there,   and a lot of options even within File::Find.   I suggest that you step back one or two paces and consider what you are trying to accomplish, and then consider what might be the easiest way to do it.   Perhaps you are trying just a little bit too earnestly to get this particular approach to work ... and, “lawdy knows” I’ve done exactly the same thing!   (Show of hands, please?   Thank you.   See what I mean...?)