[Pdx-pm] recursive IO::Dir alternatives

Ingy dot Net ingy at ingy.net
Fri Feb 19 13:04:40 PST 2010


IO::All works too. Here's some tasty idioms taken from the doc page:

           # Read all the files/directories in a directory:
           $io = io('my/directory/');                  # Create new
directory object
           @contents = $io->all;                       # Get all contents of
dir
           @contents = @$io;                           # Directory as an
array
           @contents = values %$io;                    # Directory as a hash
           push @contents, $subdir                     # One at a time
             while $subdir = $io->next;

           # Print the name and file type for all the contents above:
           print "$_ is a " . $_->type . "\n"          # Each element of
@contents
             for @contents;                            # is an IO::All
object!!

           # Print first line of each file:
           print $_->getline                           # getline gets one
line
             for io('dir')->all_files;                 # Files only

           # Print names of all files/dirs three directories deep:
           print "$_\n" for $io->all(3);               # Pass in the depth.
Default=1

           # Print names of all files/dirs recursively:
           print "$_\n" for $io->all(0);               # Zero means all the
way down
           print "$_\n" for $io->All;                  # Capitalized
shortcut
           print "$_\n" for $io->deep->all;            # Another way


2010/2/18 Tom Keller <kellert at ohsu.edu>

> Summary for modules to traverse a directory tree:
> 0. q(don't roll your own. ... I know that, just needed the exercise.)
> 1. File::Next
> 2. File::Fu
> 3. File::Find::Object
> 4. File::Finder
> 5. File::Find
>
> thanks,
> Tom
> kellert at ohsu.edu
> 503-494-2442
>
>
>
>
>
>
>
> On Feb 18, 2010, at 11:40 AM, Eric Wilhelm wrote:
>
> # from Tom Keller
> # on Thursday 18 February 2010 11:04:
>
> I need to be able to process files within a directory tree.
>
>
> This will give you all of the files and directories under $topdir:
>
>  my @paths = File::Fu->dir($topdir)->find(sub{1});
>
> If your matcher sub returns true, the file is returned.  So for only the
> files:
>
>  my @files = File::Fu->dir($topdir)->find(sub{$_->is_file});
>
> The filenames returned are File::Fu::File objects relative to wherever
> $topdir is relative to (i.e. absolute if $topdir is absolute.)
>
> If you want an iterator instead of one big list, you can use the
> finder() method.
>
>  my $iter = File::Fu->dir($topdir)->finder(sub{1});
>
>  while(defined(my $path = $iter->())) {
>    $path or next;
>    do_something_with($path);
>  }
>
> http://search.cpan.org/~ewilhelm/File-Fu-v0.0.6/lib/File/Fu/Dir.pm#finder
>
> There is currently some better documentation for find(), finder(), and
> the knob in my svn trunk:
>
>  http://svn.scratchcomputing.com/File-Fu/trunk/lib/File/Fu/Dir.pm
>
> --Eric
> --
> Issues of control, repair, improvement, cost, or just plain
> understandability all come down strongly in favor of open source
> solutions to complex problems of any sort.
> --Robert G. Brown
> ---------------------------------------------------
>    http://scratchcomputing.com
> ---------------------------------------------------
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list
>
>
>
> _______________________________________________
> Pdx-pm-list mailing list
> Pdx-pm-list at pm.org
> http://mail.pm.org/mailman/listinfo/pdx-pm-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/pdx-pm-list/attachments/20100219/86513ad7/attachment.html>


More information about the Pdx-pm-list mailing list