| 1 | 7 | ▶ | use 5.006; # our | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package PPIx::Element::Package; | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 7 | our $VERSION = '0.001000'; # TRIAL | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # ABSTRACT: Derive the package an element is defined in | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 7 | our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 7 | ▶ | use Scalar::Util qw( refaddr ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 7 | ▶ | use Exporter 5.57 qw( import ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 7 | our @EXPORT_OK = qw( identify_package identify_package_namespace identify_package_in_previous_siblings ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 221 | ▶ | sub identify_package { | ||||||||||||||||||||||||||
|
Subroutine
identify_package: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 221 | my ($element) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Packages are their own owners | ||||||||||||||||||||||||||||
| 30 | 221 | ▶ | return $element if $element->isa('PPI::Statement::Package'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Elements without parents have no Package | ||||||||||||||||||||||||||||
| 33 | 216 | ▶ | return unless $element->can('parent') and my $parent = $element->parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$element->can("parent") and my $parent = $element->parent
MC/DC50%1/2
$element->can("parent") and my $parent = $element->parent
$element->can("parent")
my $parent = $element->parent
Truth table66%2/3
A and B
A= $element->can("parent")B= my $parent = $element->parent
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Any element which is directly a child of a Package is also the Package | ||||||||||||||||||||||||||||
| 36 | # ( These are the package tokens themselves ) | ||||||||||||||||||||||||||||
| 37 | 192 | ▶ | return $parent if $parent->isa('PPI::Statement::Package'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Check any sibling nodes previous to the current one | ||||||||||||||||||||||||||||
| 40 | # and return the nearest Package, if present. | ||||||||||||||||||||||||||||
| 41 | 172 | my $package = identify_package_in_previous_siblings($element); | |||||||||||||||||||||||||||
| 42 | 172 | ▶ | return $package if defined $package; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # Otherwise, recursively assume the Package of whatever your | ||||||||||||||||||||||||||||
| 45 | # parent is. | ||||||||||||||||||||||||||||
| 46 | 132 | return identify_package($parent); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 89 | ▶ | sub identify_package_namespace { | ||||||||||||||||||||||||||
|
Subroutine
identify_package_namespace: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 89 | my ($element) = @_; | |||||||||||||||||||||||||||
| 63 | 89 | my $package = identify_package($element); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 89 | ▶ | return ( defined $package and defined $package->namespace ) ? $package->namespace : 'main'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $package and defined $package->namespace
MC/DC50%1/2
defined $package and defined $package->namespace
defined $package
defined $package->namespace
Truth table66%2/3
A and B
A= defined $packageB= defined $package->namespace
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 172 | ▶ | sub identify_package_in_previous_siblings { | ||||||||||||||||||||||||||
|
Subroutine
identify_package_in_previous_siblings: called CC=7 SCAR=20.0
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | 172 | my ($element) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # elements without parents or children can't hold siblings | ||||||||||||||||||||||||||||
| 84 | 172 | ▶ | return unless $element->can('parent') and my $parent = $element->parent; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$element->can("parent") and my $parent = $element->parent
MC/DC0%0/2
$element->can("parent") and my $parent = $element->parent
$element->can("parent")
my $parent = $element->parent
Truth table33%1/3
A and B
A= $element->can("parent")B= my $parent = $element->parent
| |||||||||||||||||||||||||||||
| 85 | 172 | ▶ | return unless $parent->can('children'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 172 | my $self_addr = refaddr($element); | |||||||||||||||||||||||||||
| 88 | 172 | my $last_package; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 172 | for my $sibling ( $parent->children ) { | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | # Record most recently found Package | ||||||||||||||||||||||||||||
| 93 | 648 | ▶ | $last_package = $sibling if $sibling->isa('PPI::Statement::Package'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # Return whatever package was found as soon as we find ourselves | ||||||||||||||||||||||||||||
| 96 | # ( Because we don't care about Packages after ourselves. | ||||||||||||||||||||||||||||
| 97 | 648 | ▶ | return $last_package if $self_addr eq refaddr($sibling); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 8 | return; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 7 | 1; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||||||||||