Skip to content

Commit

Permalink
Added File::Next::everything
Browse files Browse the repository at this point in the history
  • Loading branch information
petdance committed Jun 18, 2007
1 parent 6f40f13 commit d2ea293
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,5 +1,7 @@
Revision history for File-Next

NEXT

0.40 Fri Mar 9 21:32:15 CST 2007

[ENHANCEMENTS]
Expand Down
1 change: 1 addition & 0 deletions MANIFEST
Expand Up @@ -10,6 +10,7 @@ t/api.t
t/basic.t
t/dirs.t
t/dot.t
t/everything.t
t/follow.t
t/parms.t
t/pod-coverage.t
Expand Down
38 changes: 36 additions & 2 deletions Next.pm
Expand Up @@ -65,14 +65,22 @@ Note that the iterator will only return files, not directories.
=head2 files( { \%parameters }, @starting_points )
Returns an iterator that walks directories starting with the items
in I<@starting_points>. Each call to the iterator returns another file.
in I<@starting_points>. Each call to the iterator returns another
regular file.
=head2 dirs( { \%parameters }, @starting_points )
Returns an iterator that walks directories starting with the items
in I<@starting_points>. Each call to the iterator returns another
directory.
=head2 everything( { \%parameters }, @starting_points )
Returns an iterator that walks directories starting with the items
in I<@starting_points>. Each call to the iterator returns another
file, whether it's a regular file, directory, symlink, socket, or
whatever.
=head2 sort_standard( $a, $b )
A sort function for passing as a C<sort_files> parameter:
Expand Down Expand Up @@ -125,7 +133,7 @@ a collection of variables.
These are analogous to the same variables in L<File::Find>.
my $iter = File::Find::files( { file_filter => sub { /\.txt$/ } }, '/tmp' );
my $iter = File::Next::files( { file_filter => sub { /\.txt$/ } }, '/tmp' );
By default, the I<file_filter> is C<sub {1}>, or "all files".
Expand Down Expand Up @@ -249,6 +257,32 @@ sub dirs {
}; # iterator
}


sub everything {
my ($parms,@queue) = _setup( \%files_defaults, @_ );
my $filter = $parms->{file_filter};

return sub {
while (@queue) {
my ($dir,$file,$fullpath) = splice( @queue, 0, 3 );
if (-d $fullpath) {
unshift( @queue, _candidate_files( $parms, $fullpath ) );
}
else {
if ( $filter ) {
local $_ = $file;
local $File::Next::dir = $dir;
local $File::Next::name = $fullpath;
next if not $filter->();
}
}
return wantarray ? ($dir,$file,$fullpath) : $fullpath;
} # while

return;
}; # iterator
}

sub sort_standard($$) { return $_[0]->[1] cmp $_[1]->[1] }; ## no critic (ProhibitSubroutinePrototypes)
sub sort_reverse($$) { return $_[1]->[1] cmp $_[0]->[1] }; ## no critic (ProhibitSubroutinePrototypes)

Expand Down
1 change: 1 addition & 0 deletions t/dot.t
Expand Up @@ -24,6 +24,7 @@ NO_PARMS: {
basic.t
dirs.t
dot.t
everything.t
follow.t
parms.t
pod-coverage.t
Expand Down
61 changes: 61 additions & 0 deletions t/everything.t
@@ -0,0 +1,61 @@
#!perl -T

use strict;
use warnings;
use Test::More tests => 3;

use lib 't';
use Util;

BEGIN {
use_ok( 'File::Next' );
}

NO_PARMS: {
my $iter = File::Next::everything( 't/' );
isa_ok( $iter, 'CODE' );

my @actual = slurp( $iter );

my @expected = qw(
t/
t/00-load.t
t/api.t
t/basic.t
t/dirs.t
t/dot.t
t/everything.t
t/follow.t
t/parms.t
t/pod-coverage.t
t/pod.t
t/sort.t
t/swamp
t/swamp/0
t/swamp/a
t/swamp/a/a1
t/swamp/a/a2
t/swamp/b
t/swamp/b/b1
t/swamp/b/b2
t/swamp/c
t/swamp/c/c1
t/swamp/c/c2
t/swamp/c-header.h
t/swamp/c-source.c
t/swamp/javascript.js
t/swamp/Makefile
t/swamp/Makefile.PL
t/swamp/parrot.pir
t/swamp/perl-test.t
t/swamp/perl-without-extension
t/swamp/perl.pl
t/swamp/perl.pm
t/swamp/perl.pod
t/Util.pm
t/zero.t
);

@actual = grep { !/\.svn/ } @actual; # If I'm building this in my Subversion dir
sets_match( \@actual, \@expected, 'NO_PARMS' );
}

0 comments on commit d2ea293

Please sign in to comment.