Skip to content

Commit

Permalink
Added dirs()
Browse files Browse the repository at this point in the history
  • Loading branch information
petdance committed Jan 7, 2007
1 parent 3537a7b commit 0023459
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Changes
Expand Up @@ -6,6 +6,9 @@ NEXT
treated as the files or dirs they point to, but now you can
tell File::Next to ignore them.

Added a dirs() function to return an iterator that only finds
directories.


0.36 Thu Dec 21 15:50:13 CST 2006

Expand Down
40 changes: 36 additions & 4 deletions Next.pm
Expand Up @@ -87,6 +87,8 @@ These are analogous to the same variables in L<File::Find>.
By default, the I<file_filter> is C<sub {1}>, or "all files".
This filter has no effect if your iterator is only returning directories.
=head2 descend_filter => \&descend_filter
The descend_filter lets you check to see if the iterator should
Expand Down Expand Up @@ -145,10 +147,13 @@ passed in to the constructor.
=head2 files( { \%parameters }, @starting points )
Returns an iterator that walks directories starting with the items
in I<@starting_points>.
in I<@starting_points>. Each call to the iterator returns another file.
All file-finding in this module is adapted from Mark Jason Dominus'
marvelous I<Higher Order Perl>, page 126.
=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 sort_standard( $a, $b )
Expand Down Expand Up @@ -199,6 +204,7 @@ sub files {
while (@queue) {
my ($dir,$file,$fullpath) = splice( @queue, 0, 3 );

# XXX This should be in _candidate_files
if ( !$parms->{follow_symlinks} ) {
next if -l $fullpath;
}
Expand All @@ -220,6 +226,29 @@ sub files {
}; # iterator
}

sub dirs {
my $passed_parms = ref $_[0] eq 'HASH' ? {%{+shift}} : {}; # copy parm hash
my $parms = _handle_constructor_parms( $passed_parms, \%files_defaults );
my @queue = _init_queue( @_ );

return sub {
while (@queue) {
my ($dir,$file,$fullpath) = splice( @queue, 0, 3 );

# XXX This should be in _candidate_files
if ( !$parms->{follow_symlinks} ) {
next if -l $fullpath;
}
if (-d $fullpath) {
unshift( @queue, _candidate_files( $parms, $fullpath ) );
return $fullpath;
}
} # while

return;
}; # iterator
}

=for private _handle_constructor_parms( $passed_parms )
Returns a hashref of operational parameters, combined between
Expand Down Expand Up @@ -396,9 +425,12 @@ L<https://file-next.googlecode.com/svn/trunk>
=head1 ACKNOWLEDGEMENTS
All file-finding in this module is adapted from Mark Jason Dominus'
marvelous I<Higher Order Perl>, page 126.
=head1 COPYRIGHT & LICENSE
Copyright 2006 Andy Lester, all rights reserved.
Copyright 2006-2007 Andy Lester, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
Expand Down
2 changes: 1 addition & 1 deletion t/dirs.t
Expand Up @@ -2,7 +2,7 @@

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

BEGIN {
use_ok( 'File::Next' );
Expand Down
1 change: 1 addition & 0 deletions t/dot.t
Expand Up @@ -19,6 +19,7 @@ NO_PARMS: {
00-load.t
api.t
basic.t
dirs.t
dot.t
follow.t
parms.t
Expand Down
7 changes: 3 additions & 4 deletions t/parms.t
Expand Up @@ -11,7 +11,6 @@ BEGIN {


BAD_PARMS_CAUGHT: {

my @errors;
sub error_catcher {
my $error = shift;
Expand All @@ -28,18 +27,18 @@ BAD_PARMS_CAUGHT: {
}, 't/pod.t' );

is( scalar @errors, 1, 'Caught one error' );
like( $errors[0], qr/Invalid.+wango/, 'And it looks reasonable' );
like( $errors[0], qr/Invalid.+files.+wango/, 'And it looks reasonable' );
}


BAD_PARMS_UNCAUGHT: {
eval {
my $iter =
File::Next::files( {
File::Next::dirs( {
wango => 'ze tango',
}, 't/pod.t' );
};

ok( defined $@, 'Throws an error' );
like( $@, qr/Invalid.+wango/, 'And it looks reasonable' );
like( $@, qr/Invalid.+dirs.+wango/, 'And it looks reasonable' );
}

0 comments on commit 0023459

Please sign in to comment.