blib/lib/Devel/InPackage.pm

filter: Statementstmt 98.1% 53 / 54 Branchbran 80.0% 8 / 10 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 80.6% 75 / 93 CC 11 SCAR 16.9
CC
11
Cov
86%
scan
10.4
in_package
2.0
__ANON__
2.0
CRAP 5.4
↑ Summary
1 package Devel::InPackage;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $Devel::InPackage::VERSION = '0.01';
4 }
5 # ABSTRACT: scan a file for package declarations or determine which package a line belongs to
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Carp qw(confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Slurp qw(read_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 exports => ['in_package', 'scan'],
14 1 };
15  
16 1 our $VERSION;
17  
18 1 my $MODULE = qr/(?<package>[A-Za-z0-9:]+)/;
19  
20 23 ▶sub in_package {
Subroutine in_package: called CC=2 SCAR=6.9
Pod undocumented
21 23 my %args = @_;
22 # XXX: hope you don't want to know what package foo is in here:
23 # package main;
24 # { package Bar; <foo> }
25 23 ▶ my $point = delete $args{line} || confess 'need line';
Condition50%1/2
delete $args{'line'} || confess('need line')
l!l
230
MC/DC0%0/1
delete $args{'line'} || confess('need line')
delete $args{'line'}
Truth table50%1/2
A
A= delete $args{'line'}
Aresult
11
00
26  
27 23 my $result = 'main';
28 52 ▶ my $cb = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
29 52 my ($line, $package, %info) = @_;
30 52 my $line_number = $info{line_number};
31 52 ▶ if( $line_number >= $point ){
Branch: 1 branch
BranchTrueFalseTotal
if ($line_number >= $point) 23 29 52
32 23 $result = $package;
33 23 return 0;
34 }
35 29 return 1;
36 23 };
37  
38 23 scan( %args, callback => $cb);
39  
40 23 return $result;
41 }
42  
43 23 ▶sub scan {
Subroutine scan: called CC=9 SCAR=23.4
Pod undocumented
44 23 my %args = @_;
45  
46 my $program = $args{code} //
47 23 ▶ ($args{file} && read_file($args{file})) //
Condition25%2/8
($args{"code"} // $args{"file"} && read_file($args{"file"})) // confess("Need \"file\" or \"code\"")
l!l
230
$args{"code"} // $args{"file"} && read_file($args{"file"})
l!l&&r!l&&!r
2300
$args{"file"} and read_file($args{"file"})
!ll&&!rl&&r
000
MC/DC0%0/3
($args{"code"} // $args{"file"} && read_file($args{"file"})) // confess("Need \"file\" or \"code\"")
$args{"code"} $args{"file"} read_file($args{"file"})
Truth table25%1/4
A // B and C
A= $args{"code"}B= $args{"file"}C= read_file($args{"file"})
ABCresult
1XX1
0111
00X0
0100
48 confess 'Need "file" or "code"';
49  
50 23 ▶ my $callback = $args{callback} // confess 'Need "callback"';
Condition50%1/2
$args{"callback"} // confess("Need \"callback\"")
l!l
230
MC/DC0%0/1
$args{"callback"} // confess("Need \"callback\"")
$args{"callback"}
Truth table50%1/2
A
A= $args{"callback"}
Aresult
11
00
51  
52 # this is very crude, and makes incorrect assumptions about Perl
53 # syntax
54 23 my @state = ('main');
55 23 my $line_no = 0;
56 23 ▶ while( $program =~ /^(?<line>.+)$/mg ){
Branch: 1 branch
BranchTrueFalseTotal
if ($program =~ /^(?<line>.+)$/gm) 52 0 52
57 52 my $line = $+{line};
58 52 my $saved_line = $line;
59  
60 # skip comments
61 52 $line =~ s/#(.+)$//;
62  
63 52 ▶ while( $line =~ /(?<token>(?:
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /(?<token>(?: { | } | \bpackage \s+ $MODULE \s* ; | \b(?:class|role) \s+ $MODULE (.+)? { )) /gx) 33 52 85
64 { |
65 } |
66 \bpackage \s+ $MODULE \s* ; |
67 \b(?:class|role) \s+ $MODULE (.+)? { ))
68 /xg ){
69 33 given($+{token}){
70 33 when('{'){
71 5 push @state, $state[-1];
72 }
73 28 when('}'){
74 4 ▶ confess "Unmatched closing } at $line_no" unless @state > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless @state > 0 0 4 4
75 4 pop @state;
76 }
77 24 when(/(package|class|role) ($MODULE)/){
78 24 push @state, $+{package};
79 }
80 }
81 }
82  
83 52 my $continue_scanning = $callback->( $line, $state[-1], line_number => ++$line_no );
84 52 ▶ return if !$continue_scanning; # end early
Branch: 1 branch
BranchTrueFalseTotal
unless $continue_scanning 23 29 52
85 }
86  
87 0 return;
88 }
89  
90 1 1;
91  
92 __END__
↑ Summary