| 1 | package Directory::Scanner::StreamBuilder::Recursive; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Recrusive streaming directory iterator | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 8 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 8 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use UNIVERSAL::Object; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use Directory::Scanner::API::Stream; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 8 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 14 | 8 | our $AUTHORITY = 'cpan:STEVAN'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | use constant DEBUG => $ENV{DIR_SCANNER_STREAM_RECURSIVE_DEBUG} // 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"DIR_SCANNER_STREAM_RECURSIVE_DEBUG"} // 0
MC/DC0%0/1
$ENV{"DIR_SCANNER_STREAM_RECURSIVE_DEBUG"} // 0
$ENV{"DIR_SCANNER_STREAM_RECURSIVE_DEBUG"}
Truth table50%1/2
A
A= $ENV{"DIR_SCANNER_STREAM_RECURSIVE_DEBUG"}
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | ## ... | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 8 | ▶ | our @ISA; BEGIN { @ISA = ('UNIVERSAL::Object', 'Directory::Scanner::API::Stream') } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 8 | our %HAS; BEGIN { | |||||||||||||||||||||||||||
| 22 | %HAS = ( | ||||||||||||||||||||||||||||
| 23 | ▶ | stream => sub {}, | |||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | # internal state ... | ||||||||||||||||||||||||||||
| 25 | ▶ | _head => sub {}, | |||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 12 | ▶ | _stack => sub { [] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | 12 | ▶ | _is_done => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 12 | ▶ | _is_closed => sub { 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | ) | ||||||||||||||||||||||||||||
| 30 | 8 | ▶ | } | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | ## ... | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub BUILD { | ||||||||||||||||||||||||||||
| 35 | 12 | ▶ | my ($self, $params) = @_; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 12 | my $stream = $self->{stream}; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 12 | ▶ | (Scalar::Util::blessed($stream) && $stream->DOES('Directory::Scanner::API::Stream')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($stream) and $stream->DOES("Directory::Scanner::API::Stream")
MC/DC0%0/2
builtin::blessed($stream) and $stream->DOES("Directory::Scanner::API::Stream")
builtin::blessed($stream)
$stream->DOES("Directory::Scanner::API::Stream")
Truth table33%1/3
A and B
A= builtin::blessed($stream)B= $stream->DOES("Directory::Scanner::API::Stream")
| |||||||||||||||||||||||||||||
| 40 | || Carp::confess 'You must supply a directory stream'; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 12 | push @{$self->{_stack}} => $stream; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | sub clone { | ||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my ($self, $dir) = @_; | ||||||||||||||||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 47 | 0 | return $self->new( stream => $self->{stream}->clone( $dir ) ); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | ## accessor | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 44 | ▶ | sub head { $_[0]->{_head} } | ||||||||||||||||||||||||||
|
Subroutine
head: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 39 | ▶ | sub is_done { $_[0]->{_is_done} } | ||||||||||||||||||||||||||
|
Subroutine
is_done: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 33 | ▶ | sub is_closed { $_[0]->{_is_closed} } | ||||||||||||||||||||||||||
|
Subroutine
is_closed: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub close { | ||||||||||||||||||||||||||||
| 58 | 12 | ▶ | my $self = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
close: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 12 | ▶ | while ( my $stream = pop @{ $self->{_stack} } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | $stream->close; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 12 | $self->{_is_closed} = 1; | |||||||||||||||||||||||||||
| 63 | 12 | return; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub next { | ||||||||||||||||||||||||||||
| 67 | 95 | ▶ | my $self = $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 95 | ▶ | return if $self->{_is_done}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | Carp::confess 'Cannot call `next` on a closed stream' | ||||||||||||||||||||||||||||
| 72 | 95 | ▶ | if $self->{_is_closed}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 95 | my $next; | |||||||||||||||||||||||||||
| 75 | 95 | while (1) { | |||||||||||||||||||||||||||
| 76 | 148 | undef $next; # clear any previous values, just cause ... | |||||||||||||||||||||||||||
| 77 | 148 | $self->_log('Entering loop ... ') if DEBUG; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 148 | ▶ | if ( my $current = $self->{_stack}->[-1] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 136 | $self->_log('Stream available in stack') if DEBUG; | |||||||||||||||||||||||||||
| 81 | 136 | ▶ | if ( my $candidate = $current->next ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | # if we have a directory, prepare | ||||||||||||||||||||||||||||
| 83 | # to recurse into it the next time | ||||||||||||||||||||||||||||
| 84 | # we are called, then .... | ||||||||||||||||||||||||||||
| 85 | 83 | ▶ | if ( $candidate->is_dir ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 41 | push @{$self->{_stack}} => $current->clone( $candidate ); | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # return our successful candidate | ||||||||||||||||||||||||||||
| 90 | 83 | $next = $candidate; | |||||||||||||||||||||||||||
| 91 | 83 | last; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | else { | ||||||||||||||||||||||||||||
| 94 | 53 | $self->_log('Current stream has been exhausted, moving to next') if DEBUG; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # something, something, ... check is_done on $current here ... | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 53 | my $old = pop @{$self->{_stack}}; | |||||||||||||||||||||||||||
| 99 | 53 | ▶ | $old->close unless $old->is_closed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 53 | next; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | else { | ||||||||||||||||||||||||||||
| 104 | 12 | $self->_log('No more streams available in stack') if DEBUG; | |||||||||||||||||||||||||||
| 105 | 12 | $self->_log('Exiting loop ... DONE') if DEBUG; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 12 | $self->{_head} = undef; | |||||||||||||||||||||||||||
| 108 | 12 | $self->{_is_done} = 1; | |||||||||||||||||||||||||||
| 109 | 12 | last; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 95 | return $self->{_head} = $next; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 8 | 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | __END__ | ||||||||||||||||||||||||||||