Finder.pm

filter: Statementstmt 98.9% 96 / 97 Branchbran 83.3% 55 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 20 / 20 Podpod 100% 5 / 5 totaltotal 93.6% 176 / 188 CC 35 SCAR 28.9
CC
35
Cov
93%
_run
30.8
AUTOLOAD
5.1
_force_object
2.0
CRAP 18.0
« Finder/Steps.pm ↑ Summary
1 package File::Finder;
2  
3 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 ## no exports
10  
11 4 our $VERSION = '1.01';
12  
13 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 ## public methods:
16  
17 29 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
18 29 my $class = shift;
19 29 bless {
20 options => {},
21 steps => [],
22 }, $class;
23 }
24  
25 44 ▶sub as_wanted {
Subroutine as_wanted: called CC=1 SCAR=0.0
Pod documented
26 44 my $self = shift;
27 44 ▶ return sub { $self->_run };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 }
29  
30 use overload
31 4 ▶ '&{}' => 'as_wanted',
Subroutine BEGIN: called CC=1 SCAR=0.0
32 # '""' => sub { overload::StrVal(shift) },
33 4 ;
34  
35 30 ▶sub as_options {
Subroutine as_options: called CC=1 SCAR=0.0
Pod documented
36 30 my $self = shift;
37 30 ▶ return { %{$self->{options}}, wanted => sub { $self->_run } };
Subroutine __ANON__: called CC=1 SCAR=0.0
38 }
39  
40 25 ▶sub in {
Subroutine in: called CC=1 SCAR=0.0
Pod documented
41 25 my $self = _force_object(shift);
42  
43 ## this must return count in a scalar context
44 25 ▶ $self->collect(sub { $File::Find::name }, @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
45 }
46  
47 25 ▶sub collect {
Subroutine collect: called CC=1 SCAR=0.0
Pod documented
48 25 my $self = _force_object(shift);
49 25 my $code = shift;
50  
51 25 my @result;
52 25 ▶ my $self_store = $self->eval( sub { push @result, $code->() } );
Subroutine __ANON__: called CC=1 SCAR=0.0
53  
54 25 require File::Find;
55 25 File::Find::find($self_store->as_options, @_);
56  
57 ## this must return count in a scalar context
58 25 return @result;
59 }
60  
61 ## private methods
62  
63 1003 ▶sub _force_object {
Subroutine _force_object: called CC=2 SCAR=6.9
64 1003 my $self_or_class = shift;
65 1003 ▶ ref $self_or_class ? $self_or_class : $self_or_class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $self_or_class ? : 978 25 1003
66 }
67  
68 308 ▶sub _clone {
Subroutine _clone: called CC=1 SCAR=0.0
69 308 my $self = _force_object(shift);
70 bless {
71 308 options => {%{$self->{options}}},
72 308 steps => [@{$self->{steps}}],
73 }, ref $self;
74 }
75  
76 ## we set this to ensure that _ is correct for all tests
77 4 $File::Find::dont_use_nlink = 1;
78 ## otherwise, we have to lstat/stat($_) inside _run
79 ## thanks, tye!
80  
81 1968 ▶sub _run {
Subroutine _run: called CC=30 SCAR=34.3
82 1968 my $self = shift;
83  
84 1968 my @stat;
85 1968 ▶ @stat = stat if defined $_;
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 1925 43 1968
86  
87 1968 my @state = (1);
88 ## $state[-1]:
89 ## if 2: we're in a true state, but we've just seen a NOT
90 ## if 1: we're in a true state
91 ## if 0: we're in a false state
92 ## if -1: we're in a "skipping" state (true OR ...[here]...)
93  
94 1968 for my $step(@{$self->{steps}}) {
95  
96 ## verify underscore handle is good:
97 4410 ▶ if (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat) 4158 252 4410
98 4158 my @cache_stat = stat _;
99 4158 ▶ stat unless "@stat" eq "@cache_stat";
Branch: 1 branch
BranchTrueFalseTotal
unless join($", @stat) eq join($", @cache_stat) 77 4081 4158
100 }
101  
102 4410 ▶ if (ref $step) { # coderef
Branch: 6 branches
BranchTrueFalseTotal
if (ref $step) { } 3964 446 4410
elsif ($step eq 'or') { } 63 383 446
elsif ($step eq 'left') { } 27 356 383
elsif ($step eq 'right') { } 27 329 356
elsif ($step eq 'comma') { } 12 317 329
elsif ($step eq 'not') { } 317 0 317
103 3964 ▶ if ($state[-1] >= 1) { # true state
Branch: 1 branch
BranchTrueFalseTotal
if ($state[-1] >= 1) 2991 973 3964
104 2991 ▶ if ($self->$step) { # coderef ran returning true
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$step) { } 2065 926 2991
105 2065 ▶ if ($state[-1] == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state[-1] == 2) 155 1910 2065
106 155 $state[-1] = 0;
107 }
108 } else {
109 926 $state[-1]--; # 2 => 1, 1 => 0
110 }
111 }
112 } elsif ($step eq "or") {
113 # -1 => -1, 0 => 1, 1 => -1, 2 is error
114 63 ▶ croak "not before or?" if $state[-1] > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $state[-1] > 1 0 63 63
115 63 ▶ if ($state[-1] == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($state[-1] == 0) { } 12 51 63
elsif ($state[-1] == 1) { } 31 20 51
116 12 $state[-1] = 1;
117 } elsif ($state[-1] == 1) {
118 31 $state[-1] = -1;
119 }
120 } elsif ($step eq "left") {
121 ## start subrule
122 ## -1 => -1, 0 => -1, 1 => 1, 2 => 1
123 27 ▶ push @state, ($state[-1] >= 1) ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$state[-1] >= 1 ? : 11 16 27
124 } elsif ($step eq "right") {
125 ## end subrule
126 27 ▶ croak "right without left" unless @state > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @state > 1 0 27 27
127 27 ▶ croak "not before right" if $state[-1] > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $state[-1] > 1 0 27 27
128 27 my $result = pop @state;
129 27 ▶ if ($state[-1] >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state[-1] >= 1) 11 16 27
130 11 ▶ if ($result) { # 1 or -1, so counts as true
Branch: 1 branch
BranchTrueFalseTotal
if ($result) { } 9 2 11
131 9 ▶ if ($state[-1] == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state[-1] == 2) 2 7 9
132 2 $state[-1] = 0;
133 }
134 } else {
135 2 $state[-1]--; # 2 => 1, 1 => 0
136 }
137 }
138 } elsif ($step eq "comma") {
139 12 ▶ croak "not before comma" if $state[-1] > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $state[-1] > 1 0 12 12
140 12 ▶ if (@state < 2) { # not in parens
Branch: 1 branch
BranchTrueFalseTotal
if (@state < 2) { } 3 9 12
141 3 $state[-1] = 1; # reset to true
142 } else { # in parens, reset as if start of parens
143 9 ▶ $state[-1] = (($state[-2] >= 1) ? 1 : -1);
Branch: 1 branch
BranchTrueFalseTotal
$state[-2] >= 1 ? : 3 6 9
144 }
145 } elsif ($step eq "not") {
146 # -1 => -1, 0 => 0, 1 => 2, 2 => 1
147 317 ▶ if ($state[-1] >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state[-1] >= 1) 317 0 317
148 317 ▶ $state[-1] = $state[-1] > 1 ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$state[-1] > 1 ? : 2 315 317
149 }
150 } else {
151 0 die "internal error at $step";
152 }
153 }
154 1968 ▶ croak "left without right" unless @state == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @state == 1 0 1968 1968
155 1968 ▶ croak "trailing not" if $state[-1] > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $state[-1] > 1 0 1968 1968
156 1968 return $state[-1] != 0; # true and skipping are both true
157 }
158  
159 645 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=5 SCAR=16.3
160 645 my $self = _force_object(shift);
161  
162 645 my ($method) = our $AUTOLOAD =~ /(?:.*::)?(.*)/;
163 645 ▶ return if $method eq "DESTROY";
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 337 308 645
164  
165 308 my $clone = $self->_clone;
166  
167 ## bring in the steps
168 308 my $steps_class = $clone->_steps_class;
169 308 ▶ $steps_class =~ /[^\w:]/
Branch: 1 branch
BranchTrueFalseTotal
$steps_class =~ /[^\w:]/ and die "bad value for \$steps_class: $steps_class" 0 308 308
170 and die "bad value for \$steps_class: $steps_class";
171 308 ▶ eval "require $steps_class"; die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 308 308
172  
173 308 ▶ my $sub_method = $steps_class->can($method)
Branch: 1 branch
BranchTrueFalseTotal
my $sub_method = $steps_class->can($method) or croak("Cannot add step $method") 308 0 308
174 or croak "Cannot add step $method";
175  
176 308 push @{$clone->{steps}}, $sub_method->($clone, @_);
177 308 $clone;
178 }
179  
180 308 ▶sub _steps_class { "File::Finder::Steps" }
Subroutine _steps_class: called CC=1 SCAR=0.0
181  
182 4 1;
183 __END__
« Finder/Steps.pm ↑ Summary