Index/Search.pm

filter: Statementstmt 96.0% 73 / 76 Branchbran 73.5% 25 / 34 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 13 / 13 Podpod 75.0% 3 / 4 totaltotal 86.6% 123 / 142 CC 22 SCAR 16.1
CC
22
Cov
87%
subtopics
8.1
search
7.0
new
5.6
CRAP 5.0
« Index/Extract.pm ↑ Summary Index/Entry.pm »
1 package Pod::Index::Search;
2  
3 3 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 $VERSION = '0.14';
5  
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Search::Dict ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Pod::Index::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 ▶sub new {
Subroutine new: called CC=5 SCAR=17.2
Pod documented
14 4 my ($class, %args) = @_;
15  
16 4 my $self = bless {
17 %args,
18 }, $class;
19  
20 4 ▶ if ($self->{filename}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filename"}) 4 0 4
21 open my $fh, "<", $self->{filename}
22 4 ▶ or die "couldn't open $self->{filename}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->{"filename"} or die qq[couldn't open $self->{"filename"}: $!\n] 4 0 4
23 4 $self->{fh} = $fh;
24 }
25  
26 4 ▶ unless ($self->{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"fh"}) 0 4 4
27 0 require perlindex;
28 0 $self->{fh} = *perlindex::DATA;
29 }
30  
31 4 $self->{start} = tell $self->{fh};
32 12 ▶ $self->{filemap} ||= sub {
Subroutine __ANON__: called CC=4 SCAR=14.1
33 12 my ($podname) = @_;
34 12 my @path_elems = split /::/, $podname;
35 12 for my $inc (@INC) {
36 108 my $file = catfile($inc, @path_elems);
37 108 ▶ return "$file.pod" if -e "$file.pod";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$file.pod" 12 96 108
38 96 ▶ return "$file.pm" if -e "$file.pm";
Branch: 1 branch
BranchTrueFalseTotal
if -e "$file.pm" 0 96 96
39 }
40 0 return undef;
41 4 ▶ };
Condition33%1/3
$self->{"filemap"} ||= sub { my($podname) = @_; my @path_elems = split(/::/, $podname, 0); foreach my $inc (@INC) { my $file = catfile($inc, @path_elems); return "$file.pod" if -e "$file.pod"; return "$file.pm" if -e "$file.pm"; } ; return undef; }
l!l&&r!l&&!r
040
MC/DC0%0/2
$self->{"filemap"} ||= sub { my($podname) = @_; my @path_elems = split(/::/, $podname, 0); foreach my $inc (@INC) { my $file = catfile($inc, @path_elems); return "$file.pod" if -e "$file.pod"; return "$file.pm" if -e "$file.pm"; } ; return undef; }
$self->{"filemap"} sub { my($podname) = @_; my @path_elems = split(/::/, $podname, 0); foreach my $inc (@INC) { my $file = catfile($inc, @path_elems); return "$file.pod" if -e "$file.pod"; return "$file.pm" if -e "$file.pm"; } ; return undef; }
Truth table33%1/3
A ||= B
A= $self->{"filemap"}B= sub { my($podname) = @_; my @path_elems = split(/::/, $podname, 0); foreach my $inc (@INC) { my $file = catfile($inc, @path_elems); return "$file.pod" if -e "$file.pod"; return "$file.pm" if -e "$file.pm"; } ; return undef; }
ABresult
1X1
011
000
42  
43 4 return $self;
44 }
45  
46 6 ▶sub subtopics {
Subroutine subtopics: called CC=8 SCAR=20.9
Pod documented
47 6 my ($self, $keyword, %args) = @_;
48  
49 6 ▶ croak "need a keyword " unless defined $keyword;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keyword 0 6 6
50 6 my $fh = $self->{fh};
51  
52 6 $self->look($keyword);
53  
54 6 ▶ my $i = $self->{nocase} ? 'i' : '';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'nocase'} ? : 2 4 6
55  
56 6 my $re_filter = qr/^\Q$keyword\E/i;
57 my $re_select = $args{deep}
58 6 ▶ ? qr/^((?$i)\Q$keyword\E,.*)/
Branch: 1 branch
BranchTrueFalseTotal
$args{'deep'} ? : 1 5 6
59 : qr/^((?$i)\Q$keyword\E,[^,]*)/;
60  
61 6 local $_;
62 6 my @ret;
63 my %seen;
64 6 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 30 0 30
65 30 my ($topic) = split /\t/;
66 30 ▶ last unless $topic =~ $re_filter;
Branch: 1 branch
BranchTrueFalseTotal
unless $topic =~ /$re_filter/i 6 24 30
67
68 24 ▶ if ($topic =~ $re_select and not $seen{$1}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($topic =~ /$re_select/ and not $seen{$1}++) 12 12 24
Condition66%2/3
$topic =~ /$re_select/ and not $seen{$1}++
!ll&&!rl&&r
12012
MC/DC50%1/2
$topic =~ /$re_select/ and not $seen{$1}++
$topic =~ /$re_select/ not $seen{$1}++
Truth table66%2/3
A and not(B)
A= $topic =~ /$re_select/B= not $seen{$1}++
ABresult
0X0
100
111
69 12 push @ret, $1;
70 }
71 }
72 6 return @ret;
73 }
74  
75 # hack to make 'look' skip everything before __DATA__:
76 # everything before start always compares negatively
77 15 ▶sub look {
Subroutine look: called CC=1 SCAR=0.0
Pod undocumented
78 15 my ($self, $keyword) = @_;
79  
80 15 my $fh = $self->{fh};
81 15 my $start = $self->{start};
82  
83 # the search is case-insensitive (fold => 1), but the results are filtered
84 # later if the user wanted it case-sensitive
85 Search::Dict::look($fh, $keyword, {
86 156 ▶ comp => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
87 156 ▶ tell($fh) <= $start ? -1 : $_[0] cmp $_[1];
Branch: 1 branch
BranchTrueFalseTotal
tell $fh <= $start ? : 0 156 156
88 },
89 15 fold => 1,
90 });
91 }
92  
93 9 ▶sub search {
Subroutine search: called CC=7 SCAR=19.5
Pod documented
94 9 my ($self, $keyword) = @_;
95  
96 9 ▶ croak "need a keyword " unless defined $keyword;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $keyword 0 9 9
97 9 my $fh = $self->{fh};
98  
99 9 $self->look($keyword);
100  
101 9 local $_;
102 9 my @ret;
103 9 my $keyword_lc = lc $keyword;
104 9 my %seen;
105 9 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 22 1 23
106 22 chomp;
107 22 my ($entry, $podname, $line, $context) = split /\t/;
108 22 ▶ last unless lc $entry eq $keyword_lc;
Branch: 1 branch
BranchTrueFalseTotal
unless lc $entry eq $keyword_lc 8 14 22
109 14 ▶ next if !$self->{nocase} and $entry ne $keyword;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"nocase"} and $entry ne $keyword 2 12 14
Condition100%3/3
not $self->{"nocase"} and $entry ne $keyword
!ll&&!rl&&r
482
MC/DC100%2/2
not $self->{"nocase"} and $entry ne $keyword
not $self->{"nocase"} $entry ne $keyword
Truth table100%3/3
not(A) and B
A= not $self->{"nocase"}B= $entry ne $keyword
ABresult
0X0
100
111
110 12 ▶ next if $seen{"$podname\t$line"}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{"$podname\t$line"}++ 0 12 12
111 push @ret, Pod::Index::Entry->new(
112 keyword => $entry,
113 podname => $podname,
114 line => $line,
115 12 filename => $self->{filemap}($podname),
116 context => $context,
117 );
118 }
119 9 return @ret;
120 }
121  
122  
123 3 1;
124  
125 __END__
« Index/Extract.pm ↑ Summary Index/Entry.pm »