Match.pm

filter: Statementstmt 90.2% 83 / 92 Branchbran 90.4% 38 / 42 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 87.5% 14 / 16 Podpod 66.6% 2 / 3 totaltotal 89.9% 143 / 159 CC 25 SCAR 18.6
CC
25
Cov
91%
find
11.0
_make_predicate
9.1
_make_action
6.0
CRAP 6.4
↑ Summary Match/Sugar.pm »
1 package File::Find::Match;
2 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 RULE_PREDICATE => 0,
11 RULE_ACTION => 1,
12
13 # Author's birth year: 1985. :)
14 IGNORE => \19,
15 MATCH => \85,
16 3 };
17  
18  
19 3 our $VERSION = '1.0';
20 3 our @EXPORT = qw( IGNORE MATCH );
21 3 our @EXPORT_OK = @EXPORT;
22 3 our %EXPORT_TAGS = (
23 constants => [ @EXPORT ],
24 all => [ @EXPORT ],
25 );
26  
27 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
28 6 my ($this) = shift;
29 6 my $self = bless {}, $this;
30  
31 6 $self->_rules(@_);
32  
33 4 return $self;
34 }
35  
36 6 ▶sub _rules {
Subroutine _rules: called CC=4 SCAR=13.9
37 6 my $self = shift;
38
39 6 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 15 4 19
40 15 my ($predicate, $action) = (shift, shift);
41 15 ▶ croak "Undefined action!" unless defined $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 1 14 15
42 14 my $act = $self->_make_action($action);
43 13 ▶ if ($predicate eq 'default') {
Branch: 1 branch
BranchTrueFalseTotal
if ($predicate eq "default") 1 12 13
44 1 $self->{default} = $action;
45 1 next;
46 }
47 12 my $pred = $self->_make_predicate($predicate);
48
49 12 push @{ $self->{rules} }, [$pred, $action];
50 }
51  
52 4 return $self;
53 }
54  
55 0 ▶sub rule {
Subroutine rule: not called CC=1 SCAR=6.9
Pod undocumented
56 0 my $self = shift;
57 0 warn "rules() and rule() are deprecated! Please pass rules to new() from now on.\n";
58 0 $self->_rules(@_);
59 }
60 3 *rules = \&rule;
61  
62  
63 2 ▶sub find {
Subroutine find: called CC=11 SCAR=24.0
Pod documented
64 2 my ($self, @files) = @_;
65 2 my @rules = @{ $self->{rules} };
66  
67 2 ▶ if (exists $self->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"default"}) 1 1 2
68 1 ▶ push @rules, [ sub { 1 }, $self->{default} ];
Subroutine __ANON__: called CC=1 SCAR=0.0
69 }
70  
71 2 ▶ unless (@files) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@files) 1 1 2
72 1 @files = ('.');
73 }
74  
75 2 ▶ FILE: while (@files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) 129 2 131
76 129 my $path = shift @files;
77
78 129 RULE: foreach my $rule (@rules) {
79 706 ▶ if ($rule->[RULE_PREDICATE]->($path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->[0]->($path)) 129 577 706
80 129 ▶ my $v = $rule->[RULE_ACTION]->($path) || 0;
Condition100%2/2
$rule->[1]->($path) || 0
l!l
9237
MC/DC100%1/1
$rule->[1]->($path) || 0
$rule->[1]->($path)
Truth table100%2/2
A
A= $rule->[1]->($path)
Aresult
11
00
81 129 ▶ if (ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) 92 37 129
82 92 ▶ next FILE if $v == IGNORE;
Branch: 1 branch
BranchTrueFalseTotal
if $v == \19 2 90 92
83 90 ▶ last RULE if $v == MATCH;
Branch: 1 branch
BranchTrueFalseTotal
if $v == \85 41 49 90
84 }
85 }
86 }
87
88 127 ▶ if (-d $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $path) 41 86 127
89 41 my $dir;
90 41 opendir $dir, $path;
91
92 # read all files from $dir
93 # skip . and ..
94 # prepend $path/ to the file name.
95 # append to @files.
96 41 push @files, map { "$path/$_" } grep(!/^\.\.?$/, readdir $dir);
97
98 41 closedir $dir;
99 }
100 }
101 }
102  
103 # Take a predicate and return a coderef.
104 22 ▶sub _make_predicate {
Subroutine _make_predicate: called CC=9 SCAR=22.0
105 22 my ($self, $pred) = @_;
106 22 ▶ my $ref = ref($pred) || '';
Condition100%2/2
ref $pred || ''
l!l
148
MC/DC100%1/1
ref $pred || ''
ref $pred
Truth table100%2/2
A
A= ref $pred
Aresult
11
00
107
108 22 ▶ croak "Undefined predicate!" unless defined $pred;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pred 1 21 22
109
110 # If it is a qr// Regexp object,
111 # the predicate is the truth of the regex.
112 21 ▶ if ($ref eq 'Regexp') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'Regexp') { } 10 11 21
elsif ($ref eq 'CODE') { } 3 8 11
elsif (not $ref) { } 7 1 8
113 10 ▶ return sub { $_[0] =~ $pred };
Subroutine __ANON__: called CC=1 SCAR=0.0
114 }
115 # If it's a sub, just return it.
116 elsif ($ref eq 'CODE') {
117 3 return $pred;
118 }
119 elsif (not $ref) {
120 7 ▶ if ($pred eq 'dir') {
Branch: 2 branches
BranchTrueFalseTotal
if ($pred eq 'dir') { } 1 6 7
elsif ($pred eq 'file') { } 0 6 6
121 1 warn "the predicate 'dir' is deprecated.\n";
122 1 $pred = '-d';
123 } elsif ($pred eq 'file') {
124 0 warn "the predicate 'file' is deprecated.\n";
125 0 $pred = '-f';
126 }
127 7 my $code = eval "sub { package main; \$_ = shift; $pred }";
128 7 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 6 7
129 1 die $@;
130 }
131 6 return $code;
132 }
133 # All other values are illegal.
134 else {
135 1 croak "Predicate must be a string, code reference, or regexp reference.";
136 }
137 }
138  
139 # Take an action and return a coderef.
140 14 ▶sub _make_action {
Subroutine _make_action: called CC=4 SCAR=17.9
141 14 my ($self, $act) = @_;
142
143 14 ▶ if (UNIVERSAL::isa($act, 'UNIVERSAL')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($act, 'UNIVERSAL')) { } 0 14 14
elsif (ref $act eq 'CODE') { } 13 1 14
144 # it's an object. Does it support action?
145 0 ▶ if ($act->can('action')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($act->can('action')) { } 0 0 0
146 0 ▶ return sub { $act->action(shift) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 } else {
148 0 croak "Action object must support action() method!"
149 }
150 } elsif (ref($act) eq 'CODE') {
151 13 return $act;
152 } else {
153 1 croak "Action must be a coderef or an object.";
154 }
155 }
156  
157 3 1;
158 __END__
↑ Summary Match/Sugar.pm »