| 1 | 16 | ▶ | use 5.006; # our | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package PPIx::Element::Sub; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 16 | our $VERSION = '0.001000'; # TRIAL | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Find subroutines associated with any element. | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 16 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 16 | our @EXPORT_OK = qw( identify_sub identify_next_sub identify_associated_sub ); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 16 | ▶ | use Exporter 5.57 qw( import ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 16 | ▶ | use Scalar::Util qw( refaddr ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 108 | ▶ | sub identify_sub { | ||||||||||||||||||||||||||
|
Subroutine
identify_sub: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 108 | my ($element) = @_; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # any <sub Foo;> contains itself. | ||||||||||||||||||||||||||||
| 22 | 108 | ▶ | return $element if $element->isa('PPI::Statement::Sub'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # no parents => can't find a sub => must be main execution | ||||||||||||||||||||||||||||
| 25 | 94 | ▶ | return unless $element->can('parent') and defined( my $parent = $element->parent ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$element->can("parent") and defined(my $parent = $element->parent)
MC/DC100%2/2
$element->can("parent") and defined(my $parent = $element->parent)
$element->can("parent")
defined(my $parent = $element->parent)
Truth table100%3/3
A and B
A= $element->can("parent")B= defined(my $parent = $element->parent)
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # If we have a parent, ask it for which sub we're in. | ||||||||||||||||||||||||||||
| 28 | 76 | return identify_sub($parent); | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 81 | ▶ | sub identify_next_sub { | ||||||||||||||||||||||||||
|
Subroutine
identify_next_sub: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | 81 | my ($element) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Parentless nodes can't have siblings | ||||||||||||||||||||||||||||
| 35 | 81 | ▶ | return unless $element->can('parent') and defined( my $parent = $element->parent ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$element->can("parent") and defined(my $parent = $element->parent)
MC/DC100%2/2
$element->can("parent") and defined(my $parent = $element->parent)
$element->can("parent")
defined(my $parent = $element->parent)
Truth table100%3/3
A and B
A= $element->can("parent")B= defined(my $parent = $element->parent)
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # parents without children can't give us siblings | ||||||||||||||||||||||||||||
| 38 | # but *their* parents could | ||||||||||||||||||||||||||||
| 39 | 65 | ▶ | return identify_next_sub($parent) unless $parent->can('children'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 64 | my $own_addr = refaddr($element); | |||||||||||||||||||||||||||
| 42 | 64 | my $seen_self = 0; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Search direct siblings deeply | ||||||||||||||||||||||||||||
| 45 | 64 | for my $sibling ( $parent->children ) { | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | # Iterate forwards until self is found. | ||||||||||||||||||||||||||||
| 48 | 200 | ▶ | if ( not $seen_self ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 145 | ▶ | $seen_self = 1 if refaddr($sibling) eq $own_addr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 145 | next; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # For successive siblings | ||||||||||||||||||||||||||||
| 54 | # return the first sub found | ||||||||||||||||||||||||||||
| 55 | 55 | ▶ | return $sibling if $sibling->isa('PPI::Statement::Sub'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # If a sibling has children, a sub could be one of them | ||||||||||||||||||||||||||||
| 58 | # note: a false value == fail, but explit undef is invalid query | ||||||||||||||||||||||||||||
| 59 | 48 | ▶ | if ( $sibling->can('find_first') and my $result = $sibling->find_first('PPI::Statement::Sub') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$sibling->can("find_first") and my $result = $sibling->find_first("PPI::Statement::Sub")
MC/DC100%2/2
$sibling->can("find_first") and my $result = $sibling->find_first("PPI::Statement::Sub")
$sibling->can("find_first")
my $result = $sibling->find_first("PPI::Statement::Sub")
Truth table100%3/3
A and B
A= $sibling->can("find_first")B= my $result = $sibling->find_first("PPI::Statement::Sub")
| |||||||||||||||||||||||||||||
| 60 | 2 | return $result; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # When no sibling is a sub, assume that the sub is further down | ||||||||||||||||||||||||||||
| 65 | # the document, making it a sibling that succeeds a parent, or a descendent | ||||||||||||||||||||||||||||
| 66 | # of a parents sibling. | ||||||||||||||||||||||||||||
| 67 | 55 | return identify_next_sub($parent); | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 16 | ▶ | sub identify_associated_sub { | ||||||||||||||||||||||||||
|
Subroutine
identify_associated_sub: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 16 | my ($element) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # First, resolve to the containers, as all comments inside a | ||||||||||||||||||||||||||||
| 74 | # sub are related to the sub itself | ||||||||||||||||||||||||||||
| 75 | 16 | ▶ | if ( my $parent = identify_sub($element) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 7 | return $parent; | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Otherwise, look into the next-sibling tree to find the subsequent sub | ||||||||||||||||||||||||||||
| 80 | 9 | return identify_next_sub($element); | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 16 | 1; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||