| 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
| |||||||||||||||||||||||||||||
| 21 | open my $fh, "<", $self->{filename} | ||||||||||||||||||||||||||||
| 22 | 4 | ▶ | or die "couldn't open $self->{filename}: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 4 | $self->{fh} = $fh; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 4 | ▶ | unless ($self->{fh}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 38 | 96 | ▶ | return "$file.pm" if -e "$file.pm"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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;
}
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;
}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 50 | 6 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 6 | $self->look($keyword); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 6 | ▶ | my $i = $self->{nocase} ? 'i' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 59 | : qr/^((?$i)\Q$keyword\E,[^,]*)/; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 6 | local $_; | |||||||||||||||||||||||||||
| 62 | 6 | my @ret; | |||||||||||||||||||||||||||
| 63 | my %seen; | ||||||||||||||||||||||||||||
| 64 | 6 | ▶ | while (<$fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 30 | my ($topic) = split /\t/; | |||||||||||||||||||||||||||
| 66 | 30 | ▶ | last unless $topic =~ $re_filter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 24 | ▶ | if ($topic =~ $re_select and not $seen{$1}++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$topic =~ /$re_select/ and not $seen{$1}++
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}++
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 106 | 22 | chomp; | |||||||||||||||||||||||||||
| 107 | 22 | my ($entry, $podname, $line, $context) = split /\t/; | |||||||||||||||||||||||||||
| 108 | 22 | ▶ | last unless lc $entry eq $keyword_lc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 14 | ▶ | next if !$self->{nocase} and $entry ne $keyword; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->{"nocase"} and $entry ne $keyword
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
| |||||||||||||||||||||||||||||
| 110 | 12 | ▶ | next if $seen{"$podname\t$line"}++; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||