File Coverage

blib/lib/Sub/Info.pm
Criterion Covered Total %
statement 38 38 100.0
branch 11 14 78.5
condition 2 3 66.6
subroutine 6 6 100.0
pod 1 1 100.0
total 58 62 93.5


line stmt bran cond sub pod time code
1             package Sub::Info;
2 1     1   50280 use strict;
  1         2  
  1         28  
3 1     1   4 use warnings;
  1         1  
  1         41  
4              
5             our $VERSION = '0.002';
6              
7 1     1   6 use Carp qw/croak/;
  1         7  
  1         48  
8 1     1   5 use B();
  1         1  
  1         26  
9              
10 1     1   722 use Importer Importer => 'import';
  1         7436  
  1         9  
11             our @EXPORT_OK = qw{ sub_info };
12              
13             sub sub_info {
14 8     8 1 32250 my ($sub, @all_lines) = @_;
15 8         26 my %in = map {$_ => 1} @all_lines;
  4         16  
16              
17 8 50       47 croak "sub_info requires a coderef as its first argument"
18             unless ref($sub) eq 'CODE';
19              
20 8         50 my $cobj = B::svref_2object($sub);
21 8         60 my $name = $cobj->GV->NAME;
22 8         110 my $file = $cobj->FILE;
23 8         50 my $package = $cobj->GV->STASH->NAME;
24              
25 8         69 my $op = $cobj->START;
26 8         23 while ($op) {
27 58 100       305 push @all_lines => $op->line if $op->can('line');
28 58 100       190 last unless $op->can('next');
29 50         292 $op = $op->next;
30             }
31              
32 8         10 my ($start, $end, @lines);
33 8 50       20 if (@all_lines) {
34 8         28 @all_lines = sort { $a <=> $b } @all_lines;
  24         29  
35 8         18 ($start, $end) = ($all_lines[0], $all_lines[-1]);
36              
37             # Adjust start and end for the most common case of a multi-line block with
38             # parens on the lines before and after.
39 8 100       26 if ($start < $end) {
40 4 50 66     25 $start-- unless $start <= 1 || $in{$start};
41 4 100       20 $end++ unless $in{$end};
42             }
43 8         23 @lines = ($start, $end);
44             }
45              
46             return {
47 8         140 ref => $sub,
48             cobj => $cobj,
49             name => $name,
50             file => $file,
51             package => $package,
52             start_line => $start,
53             end_line => $end,
54             all_lines => \@all_lines,
55             lines => \@lines,
56             };
57             }
58              
59             1;
60              
61             __END__