| 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
| |||||||||||||||||||
| 40 | 15 | my ($predicate, $action) = (shift, shift); | |||||||||||||||||
| 41 | 15 | ▶ | croak "Undefined action!" unless defined $action; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 42 | 14 | my $act = $self->_make_action($action); | |||||||||||||||||
| 43 | 13 | ▶ | if ($predicate eq 'default') { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 72 | 1 | @files = ('.'); | |||||||||||||||||
| 73 | } | ||||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | 2 | ▶ | FILE: while (@files) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 76 | 129 | my $path = shift @files; | |||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 129 | RULE: foreach my $rule (@rules) { | |||||||||||||||||
| 79 | 706 | ▶ | if ($rule->[RULE_PREDICATE]->($path)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 80 | 129 | ▶ | my $v = $rule->[RULE_ACTION]->($path) || 0; | ||||||||||||||||
|
Condition100%2/2
$rule->[1]->($path) || 0
MC/DC100%1/1
$rule->[1]->($path) || 0
$rule->[1]->($path)
Truth table100%2/2
A
A= $rule->[1]->($path)
| |||||||||||||||||||
| 81 | 129 | ▶ | if (ref $v) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 82 | 92 | ▶ | next FILE if $v == IGNORE; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 83 | 90 | ▶ | last RULE if $v == MATCH; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | } | ||||||||||||||||||
| 85 | } | ||||||||||||||||||
| 86 | } | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | 127 | ▶ | if (-d $path) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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 || ''
MC/DC100%1/1
ref $pred || ''
ref $pred
Truth table100%2/2
A
A= ref $pred
| |||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | 22 | ▶ | croak "Undefined predicate!" unless defined $pred; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 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
| |||||||||||||||||||
| 144 | # it's an object. Does it support action? | ||||||||||||||||||
| 145 | 0 | ▶ | if ($act->can('action')) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 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__ | ||||||||||||||||||