in reply to Using directory handles in recursive functions

You probably want to look at the File::Recurse module. Recurse calls a function you provide for each file name encountered. Unless you're trying to stat directories, I image this should do it for you. It's also portable across Linux and NT. Here's some sample code that deleted the word 'The' from the front of all the MP3 files found:
use strict; use File::Recurse; use File::Basename; my $allcount = 0; my $mp3count = 0; my $rencount = 0; { recurse (\&myfunc, "."); printf "\n"; printf "Total Files Found = %d\n", $allcount; printf " MP3 Files Found = %d\n", $mp3count; printf "MP3 Files Renamed = %d\n", $rencount; } sub myfunc { my $newname = ""; ++$allcount; my ($name, $path, $suffix) = fileparse ($_[0], '\.mp3'); if (length ($suffix)) { ++$mp3count; $_ = $name; if ($name =~ m/^The /) { ++$rencount; s/(^The )//ge; $newname = $path . $_ . $suffix; rename $_[0], $newname; } } }

Replies are listed 'Best First'.
Re: Using directory handles in recursive functions
by davorg (Chancellor) on Jun 21, 2000 at 16:59 UTC

    File::Recurse seems to be very similar to File::Find which is part of the standard distribution. What advantages do you get from using File::Recurse?


    --
    <a href="http://www.dave.org.uk><http://www.dave.org.uk>

    European Perl Conference - Sept 22/24 2000
    <http://www.yapc.org/Europe/>
      Most likely it's because I found File::Recurse first...

      The real answer is File::Recurse allows one to set the maximum depth, and has the ability to pass a parameter with that permits easy 'statefulness'. If you were building a directory of files, and wanted to assign a unique identifier to each and every entry, then File::Recurse would be the tool.

      For your purposes, File::Find would work fine, and might be more appropriate. Since I cut'n'paste a good bit, and File::Recurse is installed, I just tend to gravitate for it.

      But being a good Perl monk, you'll explore the documentation for both, make an informed judgement, and use the best tool. Right?

      --Chris