File Coverage

blib/lib/Perl/Tags/PPI.pm
Criterion Covered Total %
statement 51 52 98.0
branch 9 12 75.0
condition 1 2 50.0
subroutine 14 14 100.0
pod 1 3 33.3
total 76 83 91.5


line stmt bran cond sub pod time code
1             package Perl::Tags::PPI;
2              
3 1     1   170589 use strict; use warnings;
  1     1   2  
  1         32  
  1         5  
  1         2  
  1         39  
4              
5 1     1   5 use base qw(Perl::Tags);
  1         1  
  1         623  
6              
7 1     1   11 use PPI;
  1         2  
  1         761  
8              
9             our $VERSION = '0.32';
10              
11             sub ppi_all {
12 1     1 0 2     my ( $self, $file ) = @_;
13              
14 1   50     9     my $doc = PPI::Document->new($file) || return;
15              
16 1         7513     $doc->index_locations;
17              
18 8         35     return map { $self->_tagify( $_, "$file" ) }
19 1 50   60   1459       @{ $doc->find(sub { $_[1]->isa("PPI::Statement") }) || [] }
  1         13  
  60         761  
20             }
21              
22             sub get_tags_for_file {
23 1     1 1 3     my ( $self, $file, @parsers ) = @_;
24              
25 1         2     my @tags = $self->ppi_all( $file );
26              
27 1         373     return @tags;
28             }
29              
30             sub _tagify {
31 8     8   13     my ( $self, $thing, $file ) = @_;
32              
33 8         42     my $class = $thing->class;
34              
35 8         31     my ( $first_line ) = split /\n/, $thing;
36              
37 8 100       203     if ( my ( $subtype ) = ( $class =~ /^PPI::Statement::(.*)$/ ) ) {
38              
39 7         14         my $method = "_tagify_" . lc($subtype);
40              
41 7 100       42         if ( $self->can($method) ) {
42 6         17             return $self->$method( $thing, $file, $first_line );
43                     }
44                 }
45              
46 2         7     return $self->_tagify_statement($thing, $file, $first_line);
47             }
48              
49             # catch all
50             sub _tagify_statement {
51 2     2   3     my ( $self, $thing, $file, $first_line ) = @_;
52              
53 2         12     return;
54             }
55              
56             sub _tagify_sub {
57 1     1   3     my ( $self, $thing, $file, $line ) = @_;
58              
59 1         6     return Perl::Tags::Tag::Sub->new(
60                     name => $thing->name,
61                     file => $file,
62                     line => $line,
63                     linenum => $thing->location->[0],
64                     pkg => $thing->guess_package
65                 );
66             }
67              
68             sub _tagify_variable {
69 1     1   4     my ( $self, $thing, $file, $line ) = @_;
70 2         160     return map {
71 1         6         Perl::Tags::Tag::Var->new(
72                         name => $_,
73                         file => $file,
74                         line => $line,
75                         linenum => $thing->location->[0],
76                       )
77                 } $thing->variables;
78             }
79              
80             sub _tagify_package {
81 1     1   2     my ( $self, $thing, $file, $line ) = @_;
82              
83 1         6     return Perl::Tags::Tag::Package->new(
84                     name => $thing->namespace,
85                     file => $file,
86                     line => $line,
87                     linenum => $thing->location->[0],
88                 );
89             }
90              
91             sub _tagify_include {
92 3     3   4     my ( $self, $thing, $file ) = @_;
93              
94 3 50       16     if ( my $module = $thing->module ) {
95 3         70         return Perl::Tags::Tag::Recurse->new(
96                         name => $module,
97                         line => "dummy",
98                     );
99                 }
100              
101 0         0     return;
102             }
103              
104             sub PPI::Statement::Sub::guess_package {
105 1     1 0 35     my ($self) = @_;
106              
107 1         1     my $temp = $self;
108 1         1     my $package;
109              
110 1         2     while (1) {
111 5 50       36         $temp = $temp->sprevious_sibling
112                       or last;
113              
114 5 100       135         if ( $temp->class eq 'PPI::Statement::Package' ) {
115 1         10             $package = $temp;
116 1         2             last;
117                     }
118                 }
119              
120 1         10     return $package;
121             }
122              
123             =head1 NAME
124            
125             Perl::Tags::PPI - use PPI to parse
126            
127             =head1 DESCRIPTION
128            
129             This is a drop-in replacement for the basic L<Perl::Tags> parser. Please see that module's
130             perldoc, and test C<t/04_ppi.t> for details.
131            
132             (Doc patches very welcome!)
133            
134             =head1 AUTHOR
135            
136             (c) Wolverian 2006
137            
138             Modifications by nothingmuch
139            
140             =cut
141              
142             1;
143