| 1 | package XML::DOM::Lite::NodeList; | ||||||||||
| 2 | |||||||||||
| 3 | =head1 NAME | ||||||||||
| 4 | |||||||||||
| 5 | XML::DOM::Lite::NodeList - blessed array ref for containing Node objects | ||||||||||
| 6 | |||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||
| 8 | |||||||||||
| 9 | $node->childNodes->insertNode($childNode, [$index]); | ||||||||||
| 10 | my @removedNodes = $node->childNodes->removeNode($childNode); | ||||||||||
| 11 | my $childNode = $node->childNodes->item($index); | ||||||||||
| 12 | my $numChilds = $node->childNodes->length; | ||||||||||
| 13 | |||||||||||
| 14 | =cut | ||||||||||
| 15 | |||||||||||
| 16 | 8 | ▶ | use overload '%{}' => \&as_hashref, fallback => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | sub new { | ||||||||||
| 19 | 263 | ▶ | my ($class, $nodes) = @_; | ||||||||
|
Subroutine
new: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 20 | 263 | my $self; | |||||||||
| 21 | 263 | ▶ | if (ref $nodes eq 'ARRAY') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 22 | # take a copy of the array | ||||||||||
| 23 | 263 | $self = bless [ @$nodes ], $class; | |||||||||
| 24 | } else { | ||||||||||
| 25 | 0 | die "usage error, $class must be constructed with an ARRAY ref"; | |||||||||
| 26 | } | ||||||||||
| 27 | 263 | return $self; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | sub as_hashref { | ||||||||||
| 31 | 2 | ▶ | my $self = shift; | ||||||||
|
Subroutine
as_hashref: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||
| 32 | 2 | my $hashref = { }; | |||||||||
| 33 | 2 | foreach my $n (@$self) { | |||||||||
| 34 | 2 | ▶ | $hashref->{$n->nodeName} = ($n->nodeValue ? $n->nodeValue : $n); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | } | ||||||||||
| 36 | 2 | return $hashref; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | sub insertNode { | ||||||||||
| 40 | 80 | ▶ | my ($self, $node, $index) = @_; | ||||||||
|
Subroutine
insertNode: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 41 | 80 | ▶ | if (defined $index) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 1 | splice(@{$self}, $index, 0, $node); | |||||||||
| 43 | } else { | ||||||||||
| 44 | 79 | push(@{$self}, $node); | |||||||||
| 45 | } | ||||||||||
| 46 | 80 | return $node; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | sub removeNode { | ||||||||||
| 50 | 4 | ▶ | my ($self, $node) = @_; | ||||||||
|
Subroutine
removeNode: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 51 | 4 | return splice (@{$self}, $self->nodeIndex($node), 1); | |||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | sub item { | ||||||||||
| 55 | 21 | ▶ | my ($self, $index) = @_; | ||||||||
|
Subroutine
item: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 56 | 21 | return $self->[$index]; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | sub length { | ||||||||||
| 60 | 145 | ▶ | my $self = shift; | ||||||||
|
Subroutine
length: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 61 | 145 | return scalar(@$self); | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | sub nodeIndex { | ||||||||||
| 65 | 15 | ▶ | my ($self, $node) = @_; | ||||||||
|
Subroutine
nodeIndex: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||
| 66 | 15 | ▶ | die "usage error" unless (scalar(@_) == 2); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 15 | my $i = 0; | |||||||||
| 68 | 15 | foreach (@$self) { | |||||||||
| 69 | 21 | ▶ | return $i if $_ eq $node; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 6 | $i++; | |||||||||
| 71 | } | ||||||||||
| 72 | 0 | return undef; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 8 | 1; | |||||||||