| 1 | package MDOM::Node; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | MDOM::Node - Abstract MDOM Node class, an Element that can contain other Elements | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 INHERITANCE | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | MDOM::Node | ||||||||||||||||||||||||||||
| 12 | isa MDOM::Element | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # Create a typical node (a Document in this case) | ||||||||||||||||||||||||||||
| 17 | my $Node = MDOM::Document->new; | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # Add an element to the node( in this case, a token ) | ||||||||||||||||||||||||||||
| 20 | my $Token = MDOM::Token::Word->new('my'); | ||||||||||||||||||||||||||||
| 21 | $Node->add_element( $Token ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # Get the elements for the Node | ||||||||||||||||||||||||||||
| 24 | my @elements = $Node->children; | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # Find all the barewords within a Node | ||||||||||||||||||||||||||||
| 27 | my $barewords = $Node->find( 'MDOM::Token::Word' ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # Find by more complex criteria | ||||||||||||||||||||||||||||
| 30 | my $my_tokens = $Node->find( sub { $_[1]->content eq 'my' } ); | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Remove all the whitespace | ||||||||||||||||||||||||||||
| 33 | $Node->prune( 'MDOM::Token::Whitespace' ); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # Remove by more complex criteria | ||||||||||||||||||||||||||||
| 36 | $Node->prune( sub { $_[1]->content eq 'my' } ); | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | The C<MDOM::Node> class provides an abstract base class for the Element | ||||||||||||||||||||||||||||
| 41 | classes that are able to contain other elements L<MDOM::Document>, | ||||||||||||||||||||||||||||
| 42 | L<MDOM::Statement>, and L<MDOM::Structure>. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | As well as those listed below, all of the methods that apply to | ||||||||||||||||||||||||||||
| 45 | L<MDOM::Element> objects also apply to C<MDOM::Node> objects. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 METHODS | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =cut | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 18 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | 18 | ▶ | use base 'MDOM::Element'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 18 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 18 | ▶ | use Scalar::Util 'refaddr'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 18 | ▶ | use List::MoreUtils (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 18 | use Params::Util '_INSTANCE', | |||||||||||||||||||||||||||
| 57 | 18 | ▶ | '_CLASS'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 18 | ▶ | use vars qw{$VERSION *_PARENT}; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | BEGIN { | ||||||||||||||||||||||||||||
| 61 | 18 | ▶ | $VERSION = '0.007'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 18 | *_PARENT = *MDOM::Element::_PARENT; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | ##################################################################### | ||||||||||||||||||||||||||||
| 70 | # The basic constructor | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | sub new { | ||||||||||||||||||||||||||||
| 73 | 201 | ▶ | my $class = ref $_[0] || $_[0]; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=8.1
Pod
undocumented
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||
| 74 | 201 | bless { children => [], lineno => $. }, $class; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | ##################################################################### | ||||||||||||||||||||||||||||
| 79 | # PDOM Methods | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =pod | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head2 scope | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | The C<scope> method returns true if the node represents a lexical scope | ||||||||||||||||||||||||||||
| 86 | boundary, or false if it does not. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | ### XS -> MDOM/XS.xs:_MDOM_Node__scope 0.903+ | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub scope { '' } | ||||||||||||||||||||||||||
|
Subroutine
scope: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =pod | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =head2 add_element $Element | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | The C<add_element> method adds a L<MDOM::Element> object to the end of a | ||||||||||||||||||||||||||||
| 98 | C<MDOM::Node>. Because Elements maintain links to their parent, an | ||||||||||||||||||||||||||||
| 99 | Element can only be added to a single Node. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | Returns true if the L<MDOM::Element> was added. Returns C<undef> if the | ||||||||||||||||||||||||||||
| 102 | Element was already within another Node, or the method is not passed | ||||||||||||||||||||||||||||
| 103 | a L<MDOM::Element> object. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | sub add_element { | ||||||||||||||||||||||||||||
| 108 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
add_element: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # Check the element | ||||||||||||||||||||||||||||
| 111 | 3 | ▶ | my $Element = _INSTANCE(shift, 'MDOM::Element') or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 3 | ▶ | $_PARENT{refaddr $Element} and return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # Add the argument to the elements | ||||||||||||||||||||||||||||
| 115 | 3 | push @{$self->{children}}, $Element; | |||||||||||||||||||||||||||
| 116 | Scalar::Util::weaken( | ||||||||||||||||||||||||||||
| 117 | 3 | $_PARENT{refaddr $Element} = $self | |||||||||||||||||||||||||||
| 118 | ); | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 3 | 1; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # In a typical run profile, add_element is the number 1 resource drain. | ||||||||||||||||||||||||||||
| 124 | # This is a highly optimised unsafe version, for internal use only. | ||||||||||||||||||||||||||||
| 125 | sub __add_element { | ||||||||||||||||||||||||||||
| 126 | Scalar::Util::weaken( | ||||||||||||||||||||||||||||
| 127 | 864 | ▶ | $_PARENT{refaddr $_[1]} = $_[0] | ||||||||||||||||||||||||||
|
Subroutine
__add_element: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||
| 129 | 864 | push @{$_[0]->{children}}, $_[1]; | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | sub __add_elements { | ||||||||||||||||||||||||||||
| 133 | 232 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__add_elements: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 134 | 232 | for (@_) { $self->__add_element($_); } | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | =pod | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 elements | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | The C<elements> method accesses all child elements B<structurally> within | ||||||||||||||||||||||||||||
| 142 | the C<MDOM::Node> object. Note that in the base of the L<MDOM::Structure> | ||||||||||||||||||||||||||||
| 143 | classes, this C<DOES> include the brace tokens at either end of the | ||||||||||||||||||||||||||||
| 144 | structure. | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | Returns a list of zero or more L<MDOM::Element> objects. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Alternatively, if called in the scalar context, the C<elements> method | ||||||||||||||||||||||||||||
| 149 | returns a count of the number of elements. | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =cut | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | sub elements { | ||||||||||||||||||||||||||||
| 154 | 13 | ▶ | wantarray ? @{$_[0]->{children}} : scalar @{$_[0]->{children}}; | ||||||||||||||||||||||||||
|
Subroutine
elements: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =pod | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =head2 first_element | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | The C<first_element> method accesses the first element structurally within | ||||||||||||||||||||||||||||
| 162 | the C<MDOM::Node> object. As for the C<elements> method, this does include | ||||||||||||||||||||||||||||
| 163 | the brace tokens for L<MDOM::Structure> objects. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | Returns a L<MDOM::Element> object, or C<undef> if for some reason the | ||||||||||||||||||||||||||||
| 166 | C<MDOM::Node> object does not contain any elements. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # Normally the first element is also the first child | ||||||||||||||||||||||||||||
| 171 | sub first_element { | ||||||||||||||||||||||||||||
| 172 | 2 | ▶ | $_[0]->{children}->[0]; | ||||||||||||||||||||||||||
|
Subroutine
first_element: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =pod | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =head2 last_element | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | The C<last_element> method accesses the last element structurally within | ||||||||||||||||||||||||||||
| 180 | the C<MDOM::Node> object. As for the C<elements> method, this does include | ||||||||||||||||||||||||||||
| 181 | the brace tokens for L<MDOM::Structure> objects. | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | Returns a L<MDOM::Element> object, or C<undef> if for some reason the | ||||||||||||||||||||||||||||
| 184 | C<MDOM::Node> object does not contain any elements. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | # Normally the last element is also the last child | ||||||||||||||||||||||||||||
| 189 | sub last_element { | ||||||||||||||||||||||||||||
| 190 | 84 | ▶ | $_[0]->{children}->[-1]; | ||||||||||||||||||||||||||
|
Subroutine
last_element: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =pod | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head2 children | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | The C<children> method accesses all child elements lexically within the | ||||||||||||||||||||||||||||
| 198 | C<MDOM::Node> object. Note that in the case of the L<MDOM::Structure> | ||||||||||||||||||||||||||||
| 199 | classes, this does B<NOT> include the brace tokens at either end of the | ||||||||||||||||||||||||||||
| 200 | structure. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Returns a list of zero of more L<MDOM::Element> objects. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Alternatively, if called in the scalar context, the C<children> method | ||||||||||||||||||||||||||||
| 205 | returns a count of the number of lexical children. | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | # In the default case, this is the same as for the elements method | ||||||||||||||||||||||||||||
| 210 | sub children { | ||||||||||||||||||||||||||||
| 211 | 10 | ▶ | wantarray ? @{$_[0]->{children}} : scalar @{$_[0]->{children}}; | ||||||||||||||||||||||||||
|
Subroutine
children: called CC=2 SCAR=8.1
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =pod | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head2 schildren | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | The C<schildren> method is really just a convenience, the significant-only | ||||||||||||||||||||||||||||
| 219 | variation of the normal C<children> method. | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | In list context, returns a list of significant children. In scalar context, | ||||||||||||||||||||||||||||
| 222 | returns the number of significant children. | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =cut | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | sub schildren { | ||||||||||||||||||||||||||||
| 227 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
schildren: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | 1 | my @schildren = grep { $_->significant } $self->children; | |||||||||||||||||||||||||||
| 229 | 1 | ▶ | wantarray ? @schildren : scalar(@schildren); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =pod | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head2 child $index | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | The C<child> method accesses a child L<MDOM::Element> object by its | ||||||||||||||||||||||||||||
| 237 | position within the Node. | ||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||
| 239 | Returns a L<MDOM::Element> object, or C<undef> if there is no child | ||||||||||||||||||||||||||||
| 240 | element at that node. | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =cut | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | sub child { | ||||||||||||||||||||||||||||
| 245 | 11 | ▶ | $_[0]->{children}->[$_[1]]; | ||||||||||||||||||||||||||
|
Subroutine
child: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =pod | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =head2 schild $index | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | The lexical structure of the Perl language ignores 'insignificant' items, | ||||||||||||||||||||||||||||
| 253 | such as whitespace and comments, while L<MDOM> treats these items as valid | ||||||||||||||||||||||||||||
| 254 | tokens so that it can reassemble the file at any time. Because of this, | ||||||||||||||||||||||||||||
| 255 | in many situations there is a need to find an Element within a Node by | ||||||||||||||||||||||||||||
| 256 | index, only counting lexically significant Elements. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | The C<schild> method returns a child Element by index, ignoring | ||||||||||||||||||||||||||||
| 259 | insignificant Elements. The index of a child Element is specified in the | ||||||||||||||||||||||||||||
| 260 | same way as for a normal array, with the first Element at index 0, and | ||||||||||||||||||||||||||||
| 261 | negative indexes used to identify a "from the end" position. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | =cut | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | sub schild { | ||||||||||||||||||||||||||||
| 266 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
schild: called CC=8 SCAR=27.2
Pod
documented
| |||||||||||||||||||||||||||||
| 267 | 2 | my $idx = 0 + shift; | |||||||||||||||||||||||||||
| 268 | 2 | my $el = $self->{children}; | |||||||||||||||||||||||||||
| 269 | 2 | ▶ | if ( $idx < 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 270 | 0 | my $cursor = 0; | |||||||||||||||||||||||||||
| 271 | 0 | ▶ | while ( exists $el->[--$cursor] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 0 | ▶ | return $el->[$cursor] if $el->[$cursor]->significant and ++$idx >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$el->[$cursor]->significant and ++$idx >= 0
MC/DC0%0/2
$el->[$cursor]->significant and ++$idx >= 0
$el->[$cursor]->significant
++$idx >= 0
Truth table0%0/3
A and B
A= $el->[$cursor]->significantB= ++$idx >= 0
| |||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | } else { | ||||||||||||||||||||||||||||
| 275 | 2 | my $cursor = -1; | |||||||||||||||||||||||||||
| 276 | 2 | ▶ | while ( exists $el->[++$cursor] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 4 | ▶ | return $el->[$cursor] if $el->[$cursor]->significant and --$idx < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$el->[$cursor]->significant and --$idx < 0
MC/DC100%2/2
$el->[$cursor]->significant and --$idx < 0
$el->[$cursor]->significant
--$idx < 0
Truth table100%3/3
A and B
A= $el->[$cursor]->significantB= --$idx < 0
| |||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||
| 280 | 0 | undef; | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | =pod | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | =head2 contains $Element | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | The C<contains> method is used to determine if another L<MDOM::Element> | ||||||||||||||||||||||||||||
| 288 | object is logically "within" a C<MDOM::Node>. For the special case of the | ||||||||||||||||||||||||||||
| 289 | brace tokens at either side of a L<MDOM::Structure> object, they are | ||||||||||||||||||||||||||||
| 290 | generally considered "within" a L<MDOM::Structure> object, even if they are | ||||||||||||||||||||||||||||
| 291 | not actually in the elements for the L<MDOM::Structure>. | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | Returns true if the L<MDOM::Element> is within us, false if not, or C<undef> | ||||||||||||||||||||||||||||
| 294 | on error. | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =cut | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | sub contains { | ||||||||||||||||||||||||||||
| 299 | 4 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
contains: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||||||||||||||||||
| 300 | 4 | ▶ | my $Element = _INSTANCE(shift, 'MDOM::Element') or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | # Iterate up the Element's parent chain until we either run out | ||||||||||||||||||||||||||||
| 303 | # of parents, or get to ourself. | ||||||||||||||||||||||||||||
| 304 | 4 | ▶ | while ( $Element = $Element->parent ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 2 | ▶ | return 1 if refaddr($self) == refaddr($Element); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 2 | ''; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =pod | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =head2 find $class | \&wanted | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | The C<find> method is used to search within a code tree for | ||||||||||||||||||||||||||||
| 316 | L<MDOM::Element> objects that meet a particular condition. | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | To specify the condition, the method can be provided with either a simple | ||||||||||||||||||||||||||||
| 319 | class name (full or shortened), or a C<CODE>/function reference. | ||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | # Find all single quotes in a Document (which is a Node) | ||||||||||||||||||||||||||||
| 322 | $Document->find('MDOM::Quote::Single'); | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | # The same thing with a shortened class name | ||||||||||||||||||||||||||||
| 325 | $Document->find('Quote::Single'); | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | # Anything more elaborate, we so with the sub | ||||||||||||||||||||||||||||
| 328 | $Document->find( sub { | ||||||||||||||||||||||||||||
| 329 | # At the top level of the file... | ||||||||||||||||||||||||||||
| 330 | $_[1]->parent == $_[0] | ||||||||||||||||||||||||||||
| 331 | and ( | ||||||||||||||||||||||||||||
| 332 | # ...find all comments and POD | ||||||||||||||||||||||||||||
| 333 | $_[1]->isa('MDOM::Token::Pod') | ||||||||||||||||||||||||||||
| 334 | or | ||||||||||||||||||||||||||||
| 335 | $_[1]->isa('MDOM::Token::Comment') | ||||||||||||||||||||||||||||
| 336 | ) | ||||||||||||||||||||||||||||
| 337 | } ); | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | The function will be passed two arguments, the top-level C<MDOM::Node> | ||||||||||||||||||||||||||||
| 340 | you are searching in and the current L<MDOM::Element> that the condition | ||||||||||||||||||||||||||||
| 341 | is testing. | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | The anonymous function should return one of three values. Returning true | ||||||||||||||||||||||||||||
| 344 | indicates a condition match, defined-false (C<0> or C<''>) indicates | ||||||||||||||||||||||||||||
| 345 | no-match, and C<undef> indicates no-match and no-descend. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | In the last case, the tree walker will skip over anything below the | ||||||||||||||||||||||||||||
| 348 | C<undef>-returning element and move on to the next element at the same | ||||||||||||||||||||||||||||
| 349 | level. | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | To halt the entire search and return C<undef> immediately, a condition | ||||||||||||||||||||||||||||
| 352 | function should throw an exception (i.e. C<die>). | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | Note that this same wanted logic is used for all methods documented to | ||||||||||||||||||||||||||||
| 355 | have a C<\&wanted> parameter, as this one does. | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | The C<find> method returns a reference to an array of L<MDOM::Element> | ||||||||||||||||||||||||||||
| 358 | objects that match the condition, false (but defined) if no Elements match | ||||||||||||||||||||||||||||
| 359 | the condition, or C<undef> if you provide a bad condition, or an error | ||||||||||||||||||||||||||||
| 360 | occurs during the search process. | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | In the case of a bad condition, a warning will be emitted as well. | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | =cut | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | sub find { | ||||||||||||||||||||||||||||
| 367 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=11 SCAR=30.8
Pod
documented
| |||||||||||||||||||||||||||||
| 368 | 3 | ▶ | my $wanted = $self->_wanted(shift) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||
| 370 | # Use a queue based search, rather than a recursive one | ||||||||||||||||||||||||||||
| 371 | 3 | my @found = (); | |||||||||||||||||||||||||||
| 372 | 3 | my @queue = $self->children; | |||||||||||||||||||||||||||
| 373 | 3 | eval { | |||||||||||||||||||||||||||
| 374 | 3 | ▶ | while ( my $Element = shift @queue ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 375 | 9 | my $rv = &$wanted( $self, $Element ); | |||||||||||||||||||||||||||
| 376 | 9 | ▶ | push @found, $Element if $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | # Support "don't descend on undef return" | ||||||||||||||||||||||||||||
| 379 | 9 | ▶ | next unless defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | # Skip if the Element doesn't have any children | ||||||||||||||||||||||||||||
| 382 | 9 | ▶ | next unless $Element->isa('MDOM::Node'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | # Depth-first keeps the queue size down and provides a | ||||||||||||||||||||||||||||
| 385 | # better logical order. | ||||||||||||||||||||||||||||
| 386 | 0 | ▶ | if ( $Element->isa('MDOM::Structure') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | 0 | ▶ | unshift @queue, $Element->finish if $Element->finish; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | 0 | unshift @queue, $Element->children; | |||||||||||||||||||||||||||
| 389 | 0 | ▶ | unshift @queue, $Element->start if $Element->start; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 390 | } else { | ||||||||||||||||||||||||||||
| 391 | 0 | unshift @queue, $Element->children; | |||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | }; | ||||||||||||||||||||||||||||
| 395 | 3 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | # Caught exception thrown from the wanted function | ||||||||||||||||||||||||||||
| 397 | 0 | return undef; | |||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 3 | ▶ | @found ? \@found : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =pod | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =head2 find_first $class | \&wanted | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | If the normal C<find> method is like a grep, then C<find_first> is | ||||||||||||||||||||||||||||
| 408 | equivalent to the L<Scalar::Util> C<first> function. | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | Given an element class or a wanted function, it will search depth-first | ||||||||||||||||||||||||||||
| 411 | through a tree until it finds something that matches the condition, | ||||||||||||||||||||||||||||
| 412 | returning the first Element that it encounters. | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | See the C<find> method for details on the format of the search condition. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | Returns the first L<MDOM::Element> object that matches the condition, false | ||||||||||||||||||||||||||||
| 417 | if nothing matches the condition, or C<undef> if given an invalid condition, | ||||||||||||||||||||||||||||
| 418 | or an error occurs. | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | =cut | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | sub find_first { | ||||||||||||||||||||||||||||
| 423 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
find_first: called CC=11 SCAR=32.2
Pod
documented
| |||||||||||||||||||||||||||||
| 424 | 2 | ▶ | my $wanted = $self->_wanted(shift) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | # Use the same queue-based search as for ->find | ||||||||||||||||||||||||||||
| 427 | 2 | my @queue = $self->children; | |||||||||||||||||||||||||||
| 428 | 2 | my $rv = eval { | |||||||||||||||||||||||||||
| 429 | 2 | ▶ | while ( my $Element = shift @queue ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | 3 | my $rv = &$wanted( $self, $Element ); | |||||||||||||||||||||||||||
| 431 | 3 | ▶ | return $Element if $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | # Support "don't descend on undef return" | ||||||||||||||||||||||||||||
| 434 | 1 | ▶ | next unless defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | # Skip if the Element doesn't have any children | ||||||||||||||||||||||||||||
| 437 | 1 | ▶ | next unless $Element->isa('MDOM::Node'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | # Depth-first keeps the queue size down and provides a | ||||||||||||||||||||||||||||
| 440 | # better logical order. | ||||||||||||||||||||||||||||
| 441 | 0 | ▶ | if ( $Element->isa('MDOM::Structure') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 442 | 0 | ▶ | unshift @queue, $Element->finish if $Element->finish; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 443 | 0 | unshift @queue, $Element->children; | |||||||||||||||||||||||||||
| 444 | 0 | ▶ | unshift @queue, $Element->start if $Element->start; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 445 | } else { | ||||||||||||||||||||||||||||
| 446 | 0 | unshift @queue, $Element->children; | |||||||||||||||||||||||||||
| 447 | } | ||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||
| 449 | }; | ||||||||||||||||||||||||||||
| 450 | 2 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 451 | # Caught exception thrown from the wanted function | ||||||||||||||||||||||||||||
| 452 | 0 | return undef; | |||||||||||||||||||||||||||
| 453 | } | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 2 | ▶ | $rv or ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =pod | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | =head2 find_any $class | \&wanted | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | The C<find_any> method is a short-circuiting true/false method that behaves | ||||||||||||||||||||||||||||
| 463 | like the normal C<find> method, but returns true as soon as it finds any | ||||||||||||||||||||||||||||
| 464 | Elements that match the search condition. | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | See the C<find> method for details on the format of the search condition. | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | Returns true if any Elements that match the condition can be found, false if | ||||||||||||||||||||||||||||
| 469 | not, or C<undef> if given an invalid condition, or an error occurs. | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =cut | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | sub find_any { | ||||||||||||||||||||||||||||
| 474 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
find_any: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 475 | 0 | my $rv = $self->find_first(@_); | |||||||||||||||||||||||||||
| 476 | 0 | ▶ | $rv ? 1 : $rv; # false or undef | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | =pod | ||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | =head2 remove_child $Element | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | If passed a L<MDOM::Element> object that is a direct child of the Node, | ||||||||||||||||||||||||||||
| 484 | the C<remove_element> method will remove the C<Element> intact, along | ||||||||||||||||||||||||||||
| 485 | with any of its children. As such, this method acts essentially as a | ||||||||||||||||||||||||||||
| 486 | 'cut' function. | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | =cut | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | sub remove_child { | ||||||||||||||||||||||||||||
| 491 | 5 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
remove_child: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 492 | 5 | ▶ | my $child = _INSTANCE(shift, 'MDOM::Element') or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | # Find the position of the child | ||||||||||||||||||||||||||||
| 495 | 5 | my $key = refaddr $child; | |||||||||||||||||||||||||||
| 496 | my $p = List::MoreUtils::firstidx { | ||||||||||||||||||||||||||||
| 497 | 7 | ▶ | refaddr $_ == $key | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 498 | 5 | } @{$self->{children}}; | |||||||||||||||||||||||||||
| 499 | 5 | ▶ | return undef unless defined $p; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | # Splice it out, and remove the child's parent entry | ||||||||||||||||||||||||||||
| 502 | 5 | splice( @{$self->{children}}, $p, 1 ); | |||||||||||||||||||||||||||
| 503 | 5 | delete $_PARENT{refaddr $child}; | |||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | 5 | $child; | |||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | =pod | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | =head2 source | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | Returns the makefile source for the current node | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | =cut | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | sub source { | ||||||||||||||||||||||||||||
| 517 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
source: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 518 | 0 | join '', map { $_->source } $self->children; | |||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | =pod | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | =head2 prune $class | \&wanted | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | The C<prune> method is used to strip L<MDOM::Element> objects out of a code | ||||||||||||||||||||||||||||
| 526 | tree. The argument is the same as for the C<find> method, either a class | ||||||||||||||||||||||||||||
| 527 | name, or an anonymous subroutine which returns true/false. Any Element | ||||||||||||||||||||||||||||
| 528 | that matches the class|wanted will be deleted from the code tree, along | ||||||||||||||||||||||||||||
| 529 | with any of its children. | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | The C<prune> method returns the number of C<Element> objects that matched | ||||||||||||||||||||||||||||
| 532 | and were removed, B<non-recursively>. This might also be zero, so avoid a | ||||||||||||||||||||||||||||
| 533 | simple true/false test on the return false of the C<prune> method. It | ||||||||||||||||||||||||||||
| 534 | returns C<undef> on error, which you probably B<should> test for. | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | =cut | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | sub prune { | ||||||||||||||||||||||||||||
| 539 | 3 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
prune: called CC=8 SCAR=21.7
Pod
documented
| |||||||||||||||||||||||||||||
| 540 | 3 | ▶ | my $wanted = $self->_wanted(shift) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | # Use a depth-first queue search | ||||||||||||||||||||||||||||
| 543 | 3 | my $pruned = 0; | |||||||||||||||||||||||||||
| 544 | 3 | my @queue = $self->children; | |||||||||||||||||||||||||||
| 545 | 3 | eval { | |||||||||||||||||||||||||||
| 546 | 3 | ▶ | while ( my $element = shift @queue ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 547 | 8 | my $rv = &$wanted( $self, $element ); | |||||||||||||||||||||||||||
| 548 | 8 | ▶ | if ( $rv ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 549 | # Delete the child | ||||||||||||||||||||||||||||
| 550 | 5 | ▶ | $element->delete or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 551 | 5 | $pruned++; | |||||||||||||||||||||||||||
| 552 | 5 | next; | |||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | # Support the undef == "don't descend" | ||||||||||||||||||||||||||||
| 556 | 3 | ▶ | next unless defined $rv; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | 3 | ▶ | if ( _INSTANCE($element, 'MDOM::Node') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 559 | # Depth-first keeps the queue size down | ||||||||||||||||||||||||||||
| 560 | 0 | unshift @queue, $element->children; | |||||||||||||||||||||||||||
| 561 | } | ||||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||
| 563 | }; | ||||||||||||||||||||||||||||
| 564 | 3 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 565 | # Caught exception thrown from the wanted function | ||||||||||||||||||||||||||||
| 566 | 0 | return undef; | |||||||||||||||||||||||||||
| 567 | } | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | 3 | $pruned; | |||||||||||||||||||||||||||
| 570 | } | ||||||||||||||||||||||||||||
| 571 | |||||||||||||||||||||||||||||
| 572 | # This method is likely to be very heavily used, to take | ||||||||||||||||||||||||||||
| 573 | # it slowly and carefuly. | ||||||||||||||||||||||||||||
| 574 | ### NOTE: Renaming this function or changing either to self will probably | ||||||||||||||||||||||||||||
| 575 | ### break File::Find::Rule::MDOM | ||||||||||||||||||||||||||||
| 576 | sub _wanted { | ||||||||||||||||||||||||||||
| 577 | 8 | ▶ | my $either = shift; | ||||||||||||||||||||||||||
|
Subroutine
_wanted: called CC=15 SCAR=42.2
| |||||||||||||||||||||||||||||
| 578 | 8 | ▶ | my $it = defined $_[0] ? shift : do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 579 | 0 | ▶ | Carp::carp('Undefined value passed as search condition') if $^W; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 580 | 0 | return undef; | |||||||||||||||||||||||||||
| 581 | }; | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | # Has the caller provided a wanted function directly | ||||||||||||||||||||||||||||
| 584 | 8 | ▶ | return $it if ref $it eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 585 | 8 | ▶ | if ( ref $it ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 586 | # No other ref types are supported | ||||||||||||||||||||||||||||
| 587 | 0 | ▶ | Carp::carp('Illegal non-CODE reference passed as search condition') if $^W; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 588 | 0 | return undef; | |||||||||||||||||||||||||||
| 589 | } | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | # The first argument should be an Element class, possibly in shorthand | ||||||||||||||||||||||||||||
| 592 | 8 | ▶ | $it = "MDOM::$it" unless substr($it, 0, 6) eq 'MDOM::'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 593 | 8 | ▶ | unless ( _CLASS($it) and $it->isa('MDOM::Element') ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
&_CLASS($it) and $it->isa("MDOM::Element")
MC/DC0%0/2
&_CLASS($it) and $it->isa("MDOM::Element")
&_CLASS($it)
$it->isa("MDOM::Element")
Truth table33%1/3
A and B
A= &_CLASS($it)B= $it->isa("MDOM::Element")
| |||||||||||||||||||||||||||||
| 594 | # We got something, but it isn't an element | ||||||||||||||||||||||||||||
| 595 | 0 | ▶ | Carp::carp("Cannot create search condition for '$it': Not a MDOM::Element") if $^W; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 596 | 0 | return undef; | |||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | # Create the class part of the wanted function | ||||||||||||||||||||||||||||
| 600 | 8 | my $wanted_class = "\n\treturn '' unless \$_[1]->isa('$it');"; | |||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | # Have we been given a second argument to check the content | ||||||||||||||||||||||||||||
| 603 | 8 | my $wanted_content = ''; | |||||||||||||||||||||||||||
| 604 | 8 | ▶ | if ( defined $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 605 | 0 | my $content = shift; | |||||||||||||||||||||||||||
| 606 | 0 | ▶ | if ( ref $content eq 'Regexp' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 607 | 0 | $content = "$content"; | |||||||||||||||||||||||||||
| 608 | } elsif ( ref $content ) { | ||||||||||||||||||||||||||||
| 609 | # No other ref types are supported | ||||||||||||||||||||||||||||
| 610 | 0 | ▶ | Carp::carp("Cannot create search condition for '$it': Not a MDOM::Element") if $^W; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 611 | 0 | return undef; | |||||||||||||||||||||||||||
| 612 | } else { | ||||||||||||||||||||||||||||
| 613 | 0 | $content = quotemeta $content; | |||||||||||||||||||||||||||
| 614 | } | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | # Complete the content part of the wanted function | ||||||||||||||||||||||||||||
| 617 | 0 | $wanted_content .= "\n\treturn '' unless defined \$_[1]->{content};"; | |||||||||||||||||||||||||||
| 618 | 0 | $wanted_content .= "\n\treturn '' unless \$_[1]->{content} =~ /$content/;"; | |||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||
| 620 | |||||||||||||||||||||||||||||
| 621 | # Create the complete wanted function | ||||||||||||||||||||||||||||
| 622 | 8 | my $code = "sub {" | |||||||||||||||||||||||||||
| 623 | . $wanted_class | ||||||||||||||||||||||||||||
| 624 | . $wanted_content | ||||||||||||||||||||||||||||
| 625 | . "\n\t1;" | ||||||||||||||||||||||||||||
| 626 | . "\n}"; | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | # Compile the wanted function | ||||||||||||||||||||||||||||
| 629 | 8 | $code = eval $code; | |||||||||||||||||||||||||||
| 630 | 8 | ▶ | (ref $code eq 'CODE') ? $code : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||
| 637 | #################################################################### | ||||||||||||||||||||||||||||
| 638 | # MDOM::Element overloaded methods | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | sub tokens { | ||||||||||||||||||||||||||||
| 641 | 0 | ▶ | map { $_->tokens } @{$_[0]->{children}}; | ||||||||||||||||||||||||||
|
Subroutine
tokens: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | ### XS -> MDOM/XS.xs:_MDOM_Element__content 0.900+ | ||||||||||||||||||||||||||||
| 645 | sub content { | ||||||||||||||||||||||||||||
| 646 | 6 | ▶ | join '', map { $_->content } @{$_[0]->{children}}; | ||||||||||||||||||||||||||
|
Subroutine
content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 647 | } | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | # Clone as normal, but then go down and relink all the _PARENT entries | ||||||||||||||||||||||||||||
| 650 | sub clone { | ||||||||||||||||||||||||||||
| 651 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 652 | 1 | my $clone = $self->SUPER::clone; | |||||||||||||||||||||||||||
| 653 | 1 | $clone->__link_children; | |||||||||||||||||||||||||||
| 654 | 1 | $clone; | |||||||||||||||||||||||||||
| 655 | } | ||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | sub location { | ||||||||||||||||||||||||||||
| 658 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
location: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 659 | 0 | ▶ | my $first = $self->{children}->[0] or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 660 | 0 | $first->location; | |||||||||||||||||||||||||||
| 661 | } | ||||||||||||||||||||||||||||
| 662 | |||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | ##################################################################### | ||||||||||||||||||||||||||||
| 668 | # Internal Methods | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | sub DESTROY { | ||||||||||||||||||||||||||||
| 671 | 202 | ▶ | local $_; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 672 | 202 | ▶ | if ( $_[0]->{children} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 673 | 78 | my @queue = $_[0]; | |||||||||||||||||||||||||||
| 674 | 78 | ▶ | while ( defined($_ = shift @queue) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 675 | 943 | ▶ | unshift @queue, @{delete $_->{children}} if $_->{children}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | # Remove all internal/private weird crosslinking so that | ||||||||||||||||||||||||||||
| 678 | # the cascading DESTROY calls will get called properly. | ||||||||||||||||||||||||||||
| 679 | 943 | %$_ = (); | |||||||||||||||||||||||||||
| 680 | } | ||||||||||||||||||||||||||||
| 681 | } | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | # Remove us from our parent node as normal | ||||||||||||||||||||||||||||
| 684 | 202 | delete $_PARENT{refaddr $_[0]}; | |||||||||||||||||||||||||||
| 685 | } | ||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||
| 687 | # Find the position of a child | ||||||||||||||||||||||||||||
| 688 | sub __position { | ||||||||||||||||||||||||||||
| 689 | 0 | ▶ | my $key = refaddr $_[1]; | ||||||||||||||||||||||||||
|
Subroutine
__position: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 690 | 0 | ▶ | List::MoreUtils::firstidx { refaddr $_ == $key } @{$_[0]->{children}}; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 691 | } | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | # Insert one or more elements before a child | ||||||||||||||||||||||||||||
| 694 | sub __insert_before_child { | ||||||||||||||||||||||||||||
| 695 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__insert_before_child: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 696 | 0 | my $key = refaddr shift; | |||||||||||||||||||||||||||
| 697 | my $p = List::MoreUtils::firstidx { | ||||||||||||||||||||||||||||
| 698 | 0 | ▶ | refaddr $_ == $key | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 699 | 0 | } @{$self->{children}}; | |||||||||||||||||||||||||||
| 700 | 0 | foreach ( @_ ) { | |||||||||||||||||||||||||||
| 701 | Scalar::Util::weaken( | ||||||||||||||||||||||||||||
| 702 | 0 | $_PARENT{refaddr $_} = $self | |||||||||||||||||||||||||||
| 703 | ); | ||||||||||||||||||||||||||||
| 704 | } | ||||||||||||||||||||||||||||
| 705 | 0 | splice( @{$self->{children}}, $p, 0, @_ ); | |||||||||||||||||||||||||||
| 706 | 0 | 1; | |||||||||||||||||||||||||||
| 707 | } | ||||||||||||||||||||||||||||
| 708 | |||||||||||||||||||||||||||||
| 709 | # Insert one or more elements after a child | ||||||||||||||||||||||||||||
| 710 | sub __insert_after_child { | ||||||||||||||||||||||||||||
| 711 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__insert_after_child: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 712 | 0 | my $key = refaddr shift; | |||||||||||||||||||||||||||
| 713 | my $p = List::MoreUtils::firstidx { | ||||||||||||||||||||||||||||
| 714 | 0 | ▶ | refaddr $_ == $key | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 715 | 0 | } @{$self->{children}}; | |||||||||||||||||||||||||||
| 716 | 0 | foreach ( @_ ) { | |||||||||||||||||||||||||||
| 717 | Scalar::Util::weaken( | ||||||||||||||||||||||||||||
| 718 | 0 | $_PARENT{refaddr $_} = $self | |||||||||||||||||||||||||||
| 719 | ); | ||||||||||||||||||||||||||||
| 720 | } | ||||||||||||||||||||||||||||
| 721 | 0 | splice( @{$self->{children}}, $p + 1, 0, @_ ); | |||||||||||||||||||||||||||
| 722 | 0 | 1; | |||||||||||||||||||||||||||
| 723 | } | ||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||
| 725 | # Replace a child | ||||||||||||||||||||||||||||
| 726 | sub __replace_child { | ||||||||||||||||||||||||||||
| 727 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__replace_child: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 728 | 0 | my $key = refaddr shift; | |||||||||||||||||||||||||||
| 729 | my $p = List::MoreUtils::firstidx { | ||||||||||||||||||||||||||||
| 730 | 0 | ▶ | refaddr $_ == $key | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 731 | 0 | } @{$self->{children}}; | |||||||||||||||||||||||||||
| 732 | 0 | foreach ( @_ ) { | |||||||||||||||||||||||||||
| 733 | Scalar::Util::weaken( | ||||||||||||||||||||||||||||
| 734 | 0 | $_PARENT{refaddr $_} = $self | |||||||||||||||||||||||||||
| 735 | ); | ||||||||||||||||||||||||||||
| 736 | } | ||||||||||||||||||||||||||||
| 737 | 0 | splice( @{$self->{children}}, $p, 1, @_ ); | |||||||||||||||||||||||||||
| 738 | 0 | 1; | |||||||||||||||||||||||||||
| 739 | } | ||||||||||||||||||||||||||||
| 740 | |||||||||||||||||||||||||||||
| 741 | # Create PARENT links for an entire tree. | ||||||||||||||||||||||||||||
| 742 | # Used when cloning or thawing. | ||||||||||||||||||||||||||||
| 743 | sub __link_children { | ||||||||||||||||||||||||||||
| 744 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
__link_children: called CC=7 SCAR=22.7
| |||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||
| 746 | # Relink all our children ( depth first ) | ||||||||||||||||||||||||||||
| 747 | 1 | my @queue = ( $self ); | |||||||||||||||||||||||||||
| 748 | 1 | ▶ | while ( my $Node = shift @queue ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 749 | # Link our immediate children | ||||||||||||||||||||||||||||
| 750 | 1 | foreach my $Element ( @{$Node->{children}} ) { | |||||||||||||||||||||||||||
| 751 | Scalar::Util::weaken( | ||||||||||||||||||||||||||||
| 752 | 3 | $_PARENT{refaddr($Element)} = $Node | |||||||||||||||||||||||||||
| 753 | ); | ||||||||||||||||||||||||||||
| 754 | 3 | ▶ | unshift @queue, $Element if $Element->isa('MDOM::Node'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 755 | } | ||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||
| 757 | # If it's a structure, relink the open/close braces | ||||||||||||||||||||||||||||
| 758 | 1 | ▶ | next unless $Node->isa('MDOM::Structure'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 759 | Scalar::Util::weaken( | ||||||||||||||||||||||||||||
| 760 | 0 | ▶ | $_PARENT{refaddr($Node->start)} = $Node | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 761 | ) if $Node->start; | ||||||||||||||||||||||||||||
| 762 | Scalar::Util::weaken( | ||||||||||||||||||||||||||||
| 763 | 0 | ▶ | $_PARENT{refaddr($Node->finish)} = $Node | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 764 | ) if $Node->finish; | ||||||||||||||||||||||||||||
| 765 | } | ||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||
| 767 | 1 | 1; | |||||||||||||||||||||||||||
| 768 | } | ||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||
| 770 | 18 | 1; | |||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||
| 772 | =pod | ||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||
| 774 | =head1 TO DO | ||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||
| 776 | - Move as much as possible to L<MDOM::XS> | ||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||
| 778 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||
| 780 | See the L<support section|MDOM/SUPPORT> in the main module. | ||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||
| 782 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||
| 784 | Adam Kennedy E<lt>adamk@cpan.orgE<gt> | ||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||
| 786 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 787 | |||||||||||||||||||||||||||||
| 788 | Copyright 2001 - 2006 Adam Kennedy. | ||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||
| 790 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 791 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 792 | |||||||||||||||||||||||||||||
| 793 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 794 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||
| 796 | =cut | ||||||||||||||||||||||||||||