| 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')
MC/DC0%0/1
delete $args{'line'} || confess('need line')
delete $args{'line'}
Truth table50%1/2
A
A= delete $args{'line'}
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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\"")
$args{"code"} // $args{"file"} && read_file($args{"file"})
$args{"file"} and read_file($args{"file"})
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"})
| |||||||||||||||||||||||||||||||||||||||
| 48 | confess 'Need "file" or "code"'; | ||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||
| 50 | 23 | ▶ | my $callback = $args{callback} // confess 'Need "callback"'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$args{"callback"} // confess("Need \"callback\"")
MC/DC0%0/1
$args{"callback"} // confess("Need \"callback\"")
$args{"callback"}
Truth table50%1/2
A
A= $args{"callback"}
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||
| 87 | 0 | return; | |||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | 1 | 1; | |||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||||||||||||