File Coverage

blib/lib/PPIx/LineToSub.pm
Criterion Covered Total %
statement 38 38 100.0
branch 7 8 87.5
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 52 55 94.5


line stmt bran cond sub pod time code
1             package PPI::Document;
2 1     1   698 use strict;
  1         2  
  1         31  
3 1     1   5 use warnings;
  1         1  
  1         307  
4              
5             sub index_line_to_sub {
6 1     1 0 11277 my $self = shift;
7 1         7 $self->index_locations;
8              
9 1         2065 my $package = 'main';
10 1         3 my $sub = 'main';
11              
12 1         2 my @lines;
13              
14 1         5 foreach my $token ( $self->tokens ) {
15              
16 80         318 my ( $line, $rowchar, $col ) = @{ $token->location };
  80         221  
17 80         955 my $statement = $token->statement;
18              
19 80         1536 $lines[$line] = [ $package, $sub ];
20 80 100       250 next unless $statement;
21 60 100       168 if ( $statement->class eq 'PPI::Statement::Sub' ) {
    100          
22 48         246 $sub = $statement->name;
23             } elsif ( $statement->class eq 'PPI::Statement::Package' ) {
24 8         81 $package = $statement->namespace;
25 8         221 $sub = 'main';
26             }
27              
28 60         1231 $lines[$line] = [ $package, $sub ];
29             }
30 1         9 $self->{__line_to_sub} = \@lines;
31             }
32              
33             sub line_to_sub {
34 13     13 0 30 my ( $self, $line ) = @_;
35 13         21 my $lines = $self->{__line_to_sub};
36 13 50       43 return ( undef, undef ) unless $lines->[$line];
37              
38 13         13 my ( $package, $sub ) = @{ $lines->[$line] };
  13         27  
39              
40 13         78 return ( $package, $sub );
41             }
42              
43             package PPIx::LineToSub;
44 1     1   19 use strict;
  1         1  
  1         31  
45 1     1   4 use warnings;
  1         2  
  1         28  
46 1     1   817 use PPI;
  1         174480  
  1         59  
47             our $VERSION = '0.33';
48              
49             1;
50              
51             __END__
52              
53             =head1 NAME
54              
55             PPIx::LineToSub - Find the package and subroutine by line
56              
57             =head1 SYNOPSIS
58              
59             use PPIx::LineToSub;
60             my $document = PPI::Document->new('t/hello.pl');
61             $document->index_line_to_sub;
62              
63             my($package, $sub) = $document->line_to_sub(1);
64              
65             =head1 DESCRIPTION
66              
67             L<PPIx::LineToSub> is a module which, given a Perl file and a line
68             number, will return the package and sub in effect.
69              
70             =head1 SEE ALSO
71              
72             L<PPI>.
73              
74             =head1 AUTHOR
75              
76             Leon Brocard, C<< <acme@astray.com> >>
77              
78             =head1 COPYRIGHT
79              
80             Copyright (C) 2008, Leon Brocard
81              
82             This module is free software; you can redistribute it or modify it
83             under the same terms as Perl itself.
84              
85              
86