| 1 | package Data::Stream::Bulk::Path::Class; | ||||||||||||||||||||||||||||
| 2 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 2 | $Data::Stream::Bulk::Path::Class::AUTHORITY = 'cpan:NUFFIN'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | { | ||||||||||||||||||||||||||||
| 6 | 2 | $Data::Stream::Bulk::Path::Class::VERSION = '0.11'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: L<Path::Class::Dir> traversal | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Path::Class; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use namespace::clean -except => 'meta'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | with qw(Data::Stream::Bulk); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | has dir => ( | |||||||||||||||||||||||||||
| 19 | isa => "Path::Class::Dir", | ||||||||||||||||||||||||||||
| 20 | is => "ro", | ||||||||||||||||||||||||||||
| 21 | required => 1, | ||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 2 | has depth_first => ( | |||||||||||||||||||||||||||
| 25 | isa => "Bool", | ||||||||||||||||||||||||||||
| 26 | is => "rw", | ||||||||||||||||||||||||||||
| 27 | default => 1, | ||||||||||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 2 | has only_files => ( | |||||||||||||||||||||||||||
| 31 | isa => "Bool", | ||||||||||||||||||||||||||||
| 32 | is => "ro", | ||||||||||||||||||||||||||||
| 33 | ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 2 | has chunk_size => ( | |||||||||||||||||||||||||||
| 36 | isa => "Int", | ||||||||||||||||||||||||||||
| 37 | is => "rw", | ||||||||||||||||||||||||||||
| 38 | default => 250, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | has _stack => ( | ||||||||||||||||||||||||||||
| 42 | isa => "ArrayRef", | ||||||||||||||||||||||||||||
| 43 | is => "ro", | ||||||||||||||||||||||||||||
| 44 | 9 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 2 | ); | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | has _queue => ( | ||||||||||||||||||||||||||||
| 48 | isa => "ArrayRef", | ||||||||||||||||||||||||||||
| 49 | is => "ro", | ||||||||||||||||||||||||||||
| 50 | lazy => 1, | ||||||||||||||||||||||||||||
| 51 | 9 | ▶ | default => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 53 | 9 | return [ $self->dir ], | |||||||||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||||||||||
| 55 | 2 | ); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 36 | ▶ | sub is_done { | ||||||||||||||||||||||||||
|
Subroutine
is_done: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 36 | my $self = shift; | |||||||||||||||||||||||||||
| 59 | return ( | ||||||||||||||||||||||||||||
| 60 | 36 | @{ $self->_stack } == 0 | |||||||||||||||||||||||||||
| 61 | and | ||||||||||||||||||||||||||||
| 62 | 36 | ▶ | @{ $self->_queue } == 0 | ||||||||||||||||||||||||||
|
Condition66%2/3
@{$self->_stack;} == 0 && @{$self->_queue;} == 0
MC/DC50%1/2
@{$self->_stack;} == 0 && @{$self->_queue;} == 0
@{$self->_stack;} == 0
@{$self->_queue;} == 0
Truth table66%2/3
A && B
A= @{$self->_stack;} == 0B= @{$self->_queue;} == 0
| |||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 273 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 273 | my $self = shift; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 273 | my $queue = $self->_queue; | |||||||||||||||||||||||||||
| 70 | 273 | my $stack = $self->_stack; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 273 | my $depth_first = $self->depth_first; | |||||||||||||||||||||||||||
| 73 | 273 | my $only_files = $self->only_files; | |||||||||||||||||||||||||||
| 74 | 273 | my $chunk_size = $self->chunk_size; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 273 | my @ret; | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | 273 | ▶ | outer: while ( @$stack ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 521 | my $frame = $stack->[-1]; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 521 | my ( $dh, $parent ) = @$frame; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 521 | ▶ | while ( defined(my $entry = $dh->read) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 885 | ▶ | next if $entry eq '.' || $entry eq '..'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$entry eq "." or $entry eq ".."
MC/DC100%2/2
$entry eq "." or $entry eq ".."
$entry eq "."
$entry eq ".."
Truth table100%3/3
A or B
A= $entry eq "."B= $entry eq ".."
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 567 | my $path = $parent->file($entry); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 567 | ▶ | if ( -d $path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 150 | my $dir = $parent->subdir($entry); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 150 | ▶ | if ( $depth_first ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 50 | unshift @$queue, $dir; | |||||||||||||||||||||||||||
| 94 | } else { | ||||||||||||||||||||||||||||
| 95 | 100 | push @$queue, $dir; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 150 | last outer; | |||||||||||||||||||||||||||
| 99 | } else { | ||||||||||||||||||||||||||||
| 100 | 417 | push @ret, $path; | |||||||||||||||||||||||||||
| 101 | 417 | ▶ | return \@ret if @ret >= $chunk_size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # we're done reading this dir | ||||||||||||||||||||||||||||
| 106 | 159 | pop @$stack; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 173 | ▶ | if ( @$queue ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 159 | my $dir = shift @$queue; | |||||||||||||||||||||||||||
| 111 | 159 | ▶ | my $dh = $dir->open || croak("Can't open directory $dir: $!"); | ||||||||||||||||||||||||||
|
Condition50%1/2
$dir->open || croak("Can't open directory ${dir}: $!")
MC/DC0%0/1
$dir->open || croak("Can't open directory ${dir}: $!")
$dir->open
Truth table50%1/2
A
A= $dir->open
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 159 | ▶ | if ( $depth_first ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 53 | push @$stack, [ $dh, $dir ]; | |||||||||||||||||||||||||||
| 115 | } else { | ||||||||||||||||||||||||||||
| 116 | 106 | unshift @$stack, [ $dh, $dir ]; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 159 | ▶ | unless ( $only_files ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 106 | push @ret, $dir; | |||||||||||||||||||||||||||
| 121 | 106 | ▶ | return \@ret if @ret >= $chunk_size; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 112 | redo; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 14 | ▶ | return unless @ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 5 | return \@ret; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 2 | __PACKAGE__; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =pod | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =head1 NAME | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | Data::Stream::Bulk::Path::Class - L<Path::Class::Dir> traversal | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | =head1 VERSION | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | version 0.11 | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | use Data::Stream::Bulk::Path::Class; | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | my $dir = Data::Stream::Bulk::Path::Class->new( | ||||||||||||||||||||||||||||
| 154 | dir => Path::Class::Dir->new( ... ), | ||||||||||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | This stream produces depth or breadth first traversal order recursion through | ||||||||||||||||||||||||||||
| 160 | L<Path::Class::Dir> objects. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Items are read iteratively, and a stack of open directory handles is used to | ||||||||||||||||||||||||||||
| 163 | keep track of state. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =over 4 | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =item chunk_size | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | Defaults to 250. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =item depth_first | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Chooses between depth first and breadth first traversal order. | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =item only_files | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | If true only L<Path::Class::File> items will be returned in the output streams | ||||||||||||||||||||||||||||
| 180 | (no directories). | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =back | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 METHODS | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =over 4 | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =item is_done | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Returns true when no more files are left to iterate. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =item next | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Returns the next chunk of L<Path::Class> objects | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =back | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Yuval Kogman <nothingmuch@woobling.org> | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | This software is copyright (c) 2012 by Yuval Kogman. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 207 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =cut | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | __END__ | ||||||||||||||||||||||||||||